version: 19 March, 2024

About this document


All analyses preformed with R version 4.2.2.

Basic setup of R environment


Loading required packages

For the following analyses we will require the use of a number of different R packages. We can use the following code to quickly load in the packages and install any packages not previously installed in the R console.

if (!require("pacman")) install.packages("pacman")

pacman::p_load_gh("pmartinezarbizu/pairwiseAdonis/pairwiseAdonis", "ropensci/rnaturalearthhires", "KarstensLab/microshades")

pacman::p_load("cowplot", "car", "ggrepel", "ggspatial", "paletteer", "patchwork", "rgdal", "rnaturalearth", "sf", "Hmisc", "MCMC.OTU", "pairwiseAdonis", "RColorBrewer", "Redmonder", "flextable", "lubridate", "officer", "adegenet", "dendextend", "gdata", "ggdendro", "hierfstat", "kableExtra", "poppr", "reshape2", "StAMPP", "vcfR", "vegan", "boa", "magick", "rgeos", "sdmpredictors", "ggcorrplot", "tidyverse", "TeachingDemos", "LaplacesDemon", "adespatial", "ggnewscale", "ggbeeswarm", "multcomp", "rstatix", "R.utils", "graph4lg")

options("scipen" = 10)

# load("fknmsSint.RData")


Making color palettes to use throughout all plots

# flPal = c(paletteer_d("vapoRwave::jazzCup")[c(1, 3:4)], "#4B31B3")
# flPal = c("#D72000", "#FFAD0A", "#1BB6AF", "#132157")
# flPal = c("#E73F74", "#F2B701", "#3969AC", "#7F3C8D")

flPal = paletteer_c("viridis::turbo", n = 9, direction = -1)[c(6:9)]

boundPal = c("gray30", paletteer_d("vapoRwave::vapoRwave")[10])

pink = "#FF6A8BFF"

purple = paletteer_d("vapoRwave::vapoRwave")[10]

kColPal = c(paletteer_d("rcartocolor::BluYl")[c(7, 5, 3)], "#f5e97a", "azure3")

profPal = rev(c(microshades_palette("micro_green", 5), microshades_palette("micro_cvd_turquoise", 5),  microshades_palette("micro_cvd_orange", 3),microshades_palette("micro_cvd_purple", 1, lightest = F), microshades_palette("micro_purple", 5)))

colPalZoox = c("#807dba", "#F09163", "#48C9B0", "#FEEDA0")


Sampling info


Map of study sites


fknmsSites = read.csv("../data/stephanocoeniaMetaData.csv", header = TRUE)
fknmsSites$depthZone = factor(fknmsSites$depthZone)
fknmsSites$depthZone = factor(fknmsSites$depthZone, levels = levels(fknmsSites$depthZone)[c(2,1)])

fknmsSites$site = factor(fknmsSites$site)
fknmsSites$site = factor(fknmsSites$site, levels = levels(fknmsSites$site)[c(4, 1, 3, 2)])
fknmsSites$date = mdy(fknmsSites$date) %>% format("%d %b %Y")

fknmsPops = fknmsSites %>% group_by(site) %>% summarise(latDD = mean(latDD), longDD = mean(longDD), n = n()) %>% droplevels()

fknmsSampleSites = fknmsSites %>% group_by(site, siteID, depthZone) %>% summarise(latDD = min(latDD), longDD = min(longDD))
## `summarise()` has grouped output by 'site', 'siteID'. You can override using the
## `.groups` argument.
fknmsBounds = read.csv("../data/shp/fknmsSPA.csv", header = TRUE)

states = st_as_sf(ne_states(country = c("United States of America")), scale = "count",  crs = 4326) %>% filter(name_en %in% c("Florida", "Georgia", "Alabama"))
countries = st_as_sf(ne_countries(country = c("Cuba", "Mexico", "The Bahamas", "Bermuda"), scale = "Large"), crs = 4326)
bahamas = read_sf("../data/shp/bahamasShoreline.shp") %>% st_transform(crs = 4326)
cuba = read_sf("../data/shp/cubaShoreline.shp") %>% st_transform(crs = 4326)
florida = read_sf("../data/shp/floridaShoreline.shp") %>% st_transform(crs = 4326)
bathy = read_sf("../data/shp/flBathy.shp") %>% st_transform(crs = 4326) %>% subset(subset = DATASET %in% c("fl_shelf", "fl_coast"))
tortugasBathy = read_sf("../data/shp/tortugasBathy.shp") %>% st_transform(crs = 4326)


Next we build a hi-res polygon of FL with the study site marked and a zoomed in map of the colony locations. We use ggspatial to add a north arrow and scale bar to the main map.

floridaMap = ggplot() +
  geom_polygon(data = fknmsBounds[fknmsBounds$type == "Sanctuary",], aes(x = long, y = lat, group = location), alpha = 0.1, fill = "black", color =  "black") +
  geom_polygon(data = fknmsBounds[fknmsBounds$location == "FKNMS2",], aes(x = long, y = lat), fill = "aliceblue", color = NA) +
  # geom_polygon(data = fknmsBounds, aes(x = long, y = lat, color = type, group = location), fill = NA, linewidth = 0) +
  scale_fill_manual(values = flPal, name = "Site") +
  scale_color_manual(values = boundPal, name = "Boundaries", labels = c("FKNMS", "SPA")) +
  geom_point(data = fknmsSites, aes(x = longDD, y = latDD, shape = depthZone), color = NA, fill = NA) +
  scale_shape_manual(values = c(21, 23), name = "Depth") +
  geom_sf(data = florida, fill = "white", linewidth = 0.15) +
  geom_sf(data = cuba, fill = "white", linewidth = 0.15) +
  geom_sf(data = bahamas, fill = "white", linewidth = 0.15) +
  geom_segment(aes(x = -80.1, y = 25.3, xend = -78.825, yend = 24.44), linewidth = 0.25) +
  geom_segment(aes(x = -80.4, y = 25, xend = -80.27, yend = 23), linewidth = 0.25) +
  geom_segment(aes(x = -81.75, y = 24.7, xend = -82.22, yend = 24.28), linewidth = 0.25) +
  geom_segment(aes(x = -81.45, y = 24.7, xend = -80.78, yend = 24.28), linewidth = 0.25) +
  geom_segment(aes(x = -83.25, y = 24.75, xend = -84.183, yend = 24.28), linewidth = 0.25) +
  geom_segment(aes(x = -82.95, y = 24.75, xend = -82.74, yend = 24.28), linewidth = 0.25) +
  geom_rect(aes(xmin = -80.4, xmax = -80.1, ymin = 25, ymax = 25.3), fill = NA, color = "black", linewidth = 0.25, alpha = 0.5) +
  geom_rect(aes(xmin = -81.75, xmax = -81.45, ymin = 24.4, ymax = 24.7), fill = NA, color = "black", linewidth = 0.25, alpha = 0.5) +
  geom_rect(aes(xmin = -83.25, xmax = -82.95, ymin = 24.45, ymax = 24.75), fill = NA, color = "black", linewidth = 0.25, alpha = 0.5) +
  geom_point(data = fknmsPops, aes(x = longDD, y = latDD, fill = site), shape = 22, size = 2) +
  coord_sf(xlim = c(-84, -79), ylim = c(23, 27)) +
  scale_x_continuous(breaks = c(seq(-84, -79, by = 1))) +
  scale_y_continuous(breaks = c(seq(23, 27, by = 1))) +
  annotation_scale(location = "br", pad_x = unit(1.35, "cm"), text_pad = unit(-4, "cm")) +
  guides(fill = guide_legend(override.aes = list(shape = 22, color = "black", size = 2, stroke = 0.25), order = 1), shape = guide_legend(override.aes = list(size = c(2.25, 2), stroke = 0.25, color = "black"), order = 2), color = "none") +
  theme_bw() +
  theme(panel.background = element_rect(fill = "aliceblue"),

        plot.background = element_blank(),
        panel.border = element_rect(color = "black", size = 1, fill = NA),
        axis.title = element_blank(),
        axis.ticks = element_line(color = "black"),
        axis.text = element_text(color = "black"),
        legend.position = c(0.905, 0.875),
        legend.box.background = element_rect(linewidth = 0.35, fill = "white"),
        legend.title = element_text(color = "black", size = 8),
        legend.text = element_text(color = "black", size = 8),
        legend.spacing = unit(-5, "pt"),
        legend.key.size = unit(5, "pt"),
        legend.background = element_blank()
        )

floridaMap

largeMap = inset = ggplot() +
  geom_sf(data = states, fill = "white", linewidth = 0.3) +
  geom_sf(data = countries, fill = "white", linewidth = 0.3) +
  geom_rect(aes(xmin = -84, xmax = -79, ymin = 23, ymax = 27), color = "black", fill = NA, alpha = 0.25, linewidth = 0.5) +
  geom_rect(aes(xmin = -78.8, xmax = -77, ymin = 22.2, ymax = 22.6), fill = "aliceblue", color = NA) +
  annotation_scale(location = "bl", pad_x = unit(2.25, "cm")) +
  annotation_north_arrow(location = "tr", style = north_arrow_minimal(), pad_x = unit(-0.3, "cm")) +
  coord_sf(xlim = c(-87, -76), ylim = c(22, 31)) +
  theme_bw() +
  theme(legend.title = element_text(size = 9, face = "bold"),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        panel.background = element_rect(fill = "aliceblue"),
        panel.border = element_rect(color = "black", size = 1, fill = NA),
        legend.position = "none",
        plot.background = element_blank())

# largeMap

inset = ggplot() +
  geom_polygon(data = fknmsBounds[fknmsBounds$type == "Sanctuary",], aes(x = long, y = lat, group = location), alpha = 0.1, fill = "black", color = NA) +
   geom_polygon(data = fknmsBounds[fknmsBounds$location == "FKNMS2",], aes(x = long, y = lat), fill = "aliceblue", color = NA) +
  geom_segment(aes(x = -82.9645, xend = -82.4, y = 24.6, yend = 24.6), color = "gray92", size = .55) +
  geom_sf(data = bathy, color = "gray75", size = 0.25) +
  geom_polygon(data = fknmsBounds, aes(x = long, y = lat, color = type, group = location), fill = NA) +
  scale_fill_manual(values = flPal, name = "Site") +
  scale_color_manual(values = boundPal, name = "Boundaries", labels = c("FKNMS", "SPA")) +
  geom_point(data = fknmsSampleSites, aes(x = longDD, y = latDD, fill = site, shape = depthZone, size = depthZone)) +
  geom_sf(data = florida, fill = "white", size = 0.15) +
  scale_shape_manual(values = c(21, 23), name = "Depth") +
  scale_size_manual(values = c(1.625, 1.5)) +
  theme_bw() +
  theme(legend.title = element_text(size = 9, face = "bold"),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        panel.background = element_rect(fill = "aliceblue"),
        panel.border = element_rect(color = "black", size = 1, fill = NA),
        legend.position = "none",
        plot.background = element_blank())

# inset

upperKeys = inset +
  annotation_scale(location = "bl", pad_x = unit(1.9, "cm")) +
  coord_sf(xlim = c(-80.4, -80.1), ylim = c(25.0, 25.3)) +
  scale_x_continuous(breaks = c(seq(-80.4, -80.0, by = .1))) +
  scale_y_continuous(breaks = c(seq(25.0, 25.3, by = .1)))

lowerKeys = inset +
  annotation_scale(location = "bl", pad_x = unit(1.9, "cm")) +
  coord_sf(xlim = c(-81.75, -81.45), ylim = c(24.4, 24.7)) +
  scale_x_continuous(breaks = c(seq(-81.7, -81.3, by = .1))) +
  scale_y_continuous(breaks = c(seq(24.4, 24.7, by = .1)))

dryTortugas = ggplot() +
  geom_polygon(data = fknmsBounds[fknmsBounds$type == "Sanctuary",], aes(x = long, y = lat, group = location), alpha = 0.1, fill = "black", color = NA) +
   geom_polygon(data = fknmsBounds[fknmsBounds$location == "FKNMS2",], aes(x = long, y = lat), fill = "aliceblue", color = NA) +
  geom_segment(aes(x = -82.9645, xend = -82.4, y = 24.6, yend = 24.6), color = "gray92", size = .55) +
  geom_sf(data = tortugasBathy, color = "gray75", size = 0.25) +
  geom_polygon(data = fknmsBounds, aes(x = long, y = lat, color = type, group = location), fill = NA) +
  scale_fill_manual(values = flPal, name = "Site") +
  scale_color_manual(values = boundPal, name = "Boundaries", labels = c("FKNMS", "SPA")) +
  geom_point(data = fknmsSites, aes(x = longDD, y = latDD, fill = site, shape = depthZone, size = depthZone)) +
  geom_sf(data = florida, fill = "white", size = 0.25) +
  scale_shape_manual(values = c(21, 23), name = "Depth") +
  scale_size_manual(values = c(1.625, 1.5)) +
  annotation_scale(location = "bl", pad_x = unit(1.9, "cm")) +
  coord_sf(xlim = c(-83.25, -82.95), ylim = c(24.45, 24.75)) +
  scale_x_continuous(breaks = c(seq(-83.2, -82.9, by = .1))) +
  scale_y_continuous(breaks = c(seq(24.4, 24.7, by = .1))) +
  theme_bw() +
  theme(legend.title = element_text(size = 9, face = "bold"),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        panel.background = element_rect(fill = "aliceblue"),
        panel.border = element_rect(color = "black", size = 1, fill = NA),
        legend.position = "none",
        plot.background = element_blank())
popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "pop" = site, "depth" = depthZone, "depthm" = depthM)

popData$popdepth = as.factor(paste(popData$pop, popData$depth, sep = ""))

popData$popdepth = factor(popData$popdepth, levels(popData$popdepth)[c(4, 3, 6, 5, 2, 1, 8, 7)])

pcadmix = read.table("../data/snps/k/Clumpp3xK4.output") %>%dplyr::select(V6, V7, V8, V9)
pcadmix %>% summarise(sum(V6),sum(V7), sum(V8), sum(V9)) 
##    sum(V6) sum(V7) sum(V8) sum(V9)
## 1 128.7501 43.4095 31.2726 16.5678
pcadmix = popData %>% cbind(pcadmix) %>% rename("cluster1" = "V6", "cluster2" = "V7", "cluster3" = "V8", "cluster4" = "V9") %>%dplyr::select(order(colnames(.))) 

pcadmixClust = pcadmix %>% mutate(cluster = ifelse(cluster1 < 0.75 & cluster2 < 0.75  & cluster3 < 0.75 & cluster4 < 0.75, "NA", ifelse(cluster1 >=0.75, 1, ifelse(cluster2 >= 0.75, 2, ifelse(cluster3 >= 0.75, 3,ifelse(cluster4 >= 0.75, 4, 0))))))

pcadmix = pcadmix %>% mutate(pcadmixClust)

pcadmix$cluster = as.factor(pcadmix$cluster)
levels(pcadmix$cluster) = c("Blue", "Teal", "Green", "Yellow", "Admixed")

siteLineages = pcadmix %>% dplyr::select(popdepth, cluster) %>% 
group_by(popdepth) %>% count(cluster) %>% mutate(Freq = n/sum(n)) %>% apply(2, function(x) gsub("\\s+", "", x)) %>% as.data.frame()

pieCol = c("Blue" = kColPal[1], Teal = kColPal[2], "Green" = kColPal[3], "Yellow" = kColPal[4], "Admixed" = kColPal[5])

pieDf = siteLineages %>% group_by(popdepth) %>% mutate("ymax" = cumsum(Freq)) %>% mutate("ymin" = c(0, head(ymax, n=-1)))

ukMeso = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[1], alpha = 1) +
  geom_rect(data = pieDf%>% filter(popdepth == "UpperKeysMesophotic"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "43.9", size = 2.5, fontface = "bold", color = "black") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

lkMeso = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[2], alpha = 1) +
  geom_rect(data = pieDf%>% filter(popdepth == "LowerKeysMesophotic"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "32.8", size = 2.5, fontface = "bold") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

tbMeso = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[3], alpha = 1) +
  geom_rect(data = pieDf%>% filter(popdepth == "TortugasBankMesophotic"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "32.0", size = 2.5, fontface = "bold", color = "white") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

rhMeso = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[4], alpha = 1) +
  geom_rect(data = pieDf %>% filter(popdepth == "Riley'sHumpMesophotic"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "33.2", size = 2.5, fontface = "bold", color = "white") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

ukShal = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[1]) +
  geom_rect(data = pieDf%>% filter(popdepth == "UpperKeysShallow"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "23.6", size = 2.5, fontface = "bold", color = "black") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

lkShal = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[2]) +
  geom_rect(data = pieDf%>% filter(popdepth == "LowerKeysShallow"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", , size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "18.0", size = 2.5, fontface = "bold") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

tbShal = ggplot() +
    geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[3]) +
  geom_rect(data = pieDf%>% filter(popdepth == "TortugasBankShallow"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "21.1", size = 2.5, fontface = "bold", color = "white") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())

rhShal = ggplot() +
  geom_rect(aes(ymax=1, ymin = 0, xmax = 4, xmin = 2), fill = flPal[4]) +
  geom_rect(data = pieDf %>% filter(popdepth == "Riley'sHumpShallow"), aes(ymax=ymax, ymin = ymin, xmax = 4, xmin = 3, fill = cluster), color = "black", size = 0.25) +
  annotate(geom = "text", x = 2, y = 0.75, label = "26.4", size = 2.5, fontface = "bold", color = "white") +
  scale_fill_manual(values = pieCol)+
  coord_polar(theta = "y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none", panel.background = element_blank())
map = (floridaMap + 
  inset_element(largeMap, top = 1.01, right = 0.33, bottom = 0.63, left = -0.005, ignore_tag = TRUE) +
  inset_element(dryTortugas, top = 0.36, right = 0.2875, bottom = -0.01, left = -0.0075, ignore_tag = TRUE) +
  inset_element(lowerKeys, top = 0.36, right = 0.645, bottom = -0.01, left = 0.35, ignore_tag = TRUE) +
  inset_element(upperKeys, top = 0.395, right = 1.00, bottom = 0.025, left = 0.705, ignore_tag = TRUE) +
  inset_element(ukShal, top = 0.374, right = 0.99, bottom = 0.274, left = 0.89, ignore_tag = TRUE) +
  inset_element(ukMeso, top = 0.284, right = 0.99, bottom = 0.184, left = 0.89, ignore_tag = TRUE) +
  inset_element(lkShal, top = 0.209, right = 0.466, bottom = 0.109, left = 0.366, ignore_tag = TRUE) +
  inset_element(lkMeso, top = 0.119, right = 0.466, bottom = 0.019, left = 0.366, ignore_tag = TRUE) +  
  inset_element(rhShal, top = 0.209, right = 0.11, bottom = 0.109, left = 0.01, ignore_tag = TRUE) +
  inset_element(rhMeso, top = 0.119, right = 0.11, bottom = 0.019, left = 0.01, ignore_tag = TRUE) +
  inset_element(tbShal, top = 0.338, right = 0.278, bottom = 0.238, left = 0.178, ignore_tag = TRUE) +
  inset_element(tbMeso, top = 0.248, right = 0.278, bottom = 0.148, left = 0.178, ignore_tag = TRUE)
  )

ggsave("../figures/figure1.png", plot = map, height = 7, width = 7, units = "in", dpi = 300)

ggsave("../figures/figure1.svg", plot = map, height = 7, width = 7, units = "in", dpi = 300)


S. intersepta population genetics from SNPs


Analyzing 2bRAD generated SNPs (24,670 loci) for population structure//genetic connectivity across sites and depth zones in FKNMS

How many reads?

rawSintReads = read.delim("../data/snps/sintRawReadCounts", header = FALSE)
colnames(rawSintReads) = c("sample", "reads")

head(rawSintReads)
##    sample    reads
## 1 FKSi1-1 42167284
## 2 FKSi1-2 54651139
## 3 FKSi1-3 41635251
## 4 FKSi1-4 37754282
## 5 FKSi1-5 39973126
## 6 FKSi1-6 45580831
#total reads
sum(rawSintReads$reads)
## [1] 796139328
#average reads/sample
(sum(rawSintReads$reads)/226)
## [1] 3522740

Identifiying clonal multi-locus genotypes

Dendrogram with clones

Identification of any natural clones using technical replicates as a baseline for clonality between samples.

cloneBams = read.csv("../data/stephanocoeniaMetaData.csv") # list of bam files

# cloneMa = as.matrix(read.table("../data/snps/clones/sintClones.ibsMat")) # reads in IBS matrix produced by ANGSD 
cloneMa = as.matrix(read.table("~/Desktop/angsd3x/sintClones3x.ibsMat")) # reads in IBS matrix produced by ANGSD 

dimnames(cloneMa) = list(cloneBams[,1],cloneBams[,1])

clonePops = cloneBams$site
cloneDepth = cloneBams$depthZone

cloneDend = cloneMa %>% as.dist() %>% hclust(.,"ave") %>% as.dendrogram()
cloneDData = cloneDend %>% dendro_data()

# Making the branches hang shorter so we can easily see clonal groups
cloneDData$segments$yend2 = cloneDData$segments$yend
for(i in 1:nrow(cloneDData$segments)) {
  if (cloneDData$segments$yend2[i] == 0) {
    cloneDData$segments$yend2[i] = (cloneDData$segments$y[i] - 0.01)}}

cloneDendPoints = cloneDData$labels
cloneDendPoints$pop = clonePops[order.dendrogram(cloneDend)]
cloneDendPoints$depth=cloneDepth[order.dendrogram(cloneDend)]
rownames(cloneDendPoints) = cloneDendPoints$label

# Making points at the leaves to place symbols for populations
point = as.vector(NA)
for(i in 1:nrow(cloneDData$segments)) {
  if (cloneDData$segments$yend[i] == 0) {
    point[i] = cloneDData$segments$y[i] - 0.01
  } else {
    point[i] = NA}}

cloneDendPoints$y = point[!is.na(point)]

techReps = c("SFK066.1", "SFK066.2", "SFK066.3", "SFK162.1", "SFK162.2", "SFK162.3", "SFK205.1", "SFK205.2", "SFK205.3")

cloneDendPoints$depth = factor(cloneDendPoints$depth)
cloneDendPoints$depth = factor(cloneDendPoints$depth, levels(cloneDendPoints$depth)[c(2,1)])

cloneDendPoints$pop = factor(cloneDendPoints$pop)
cloneDendPoints$pop = factor(cloneDendPoints$pop,levels(cloneDendPoints$pop)[c(4, 1, 3, 2)])

cloneDendA = ggplot() +
  geom_rect(aes(xmin = 47.25, xmax = 50.75, ymin = 0.03, ymax = 0.085), fill = pink, alpha = 0.4) +
  geom_rect(aes(xmin = 164.25, xmax = 167.75, ymin = 0.065, ymax = 0.12), fill = pink, alpha = 0.4) +
  geom_rect(aes(xmin = 219.25, xmax = 222.75, ymin = 0.065, ymax = 0.12), fill = pink, alpha = 0.4) +
  geom_segment(data = segment(cloneDData), aes(x = x, y = y, xend = xend, yend = yend2), size = 0.5) +
  geom_point(data = cloneDendPoints, aes(x = x, y = y, fill = pop, shape = depth), size = 4, stroke = 0.25) +
  scale_fill_manual(values = flPal, name= "Site:") +
  scale_shape_manual(values = c(21, 23), name = "Depth Zone:") +
  geom_hline(yintercept = 0.12, color = pink, lty = 5, size = 1) + # creating a dashed line to indicate a clonal distance threshold
  geom_text(data = subset(cloneDendPoints, subset = label %in% techReps), aes(x = x, y = (y - .02), label = label), angle = 90) + # spacing technical replicates further from leaf
  geom_text(data = subset(cloneDendPoints, subset = !label %in% techReps), aes(x = x, y = (y - .015), label = label), angle = 90) +
  labs(y = "Genetic distance (1 - IBS)") +
  guides(fill = guide_legend(override.aes = list(shape = 22, size = 10), ncol = 2), shape = guide_legend(override.aes = list(size = 8), ncol = 1, order = 1)) +
  coord_cartesian(xlim = c(5, 218), ylim = c(0.03, 0.25)) +
  theme_classic()

cloneDend = cloneDendA + theme(
  axis.title.x = element_blank(),
  axis.text.x = element_blank(),
  axis.line.x = element_blank(),
  axis.ticks.x = element_blank(),
  axis.title.y = element_text(size = 24, color = "black", angle = 90),
  axis.text.y = element_text(size = 20, color = "black"),
  axis.line.y = element_line(),
  axis.ticks.y = element_line(),
  panel.grid = element_blank(),
  panel.border = element_blank(),
  panel.background = element_blank(),
  legend.key = element_blank(),
  legend.title = element_text(size = 24),
  legend.text = element_text(size = 20),
  legend.position = "bottom")

# cloneDend

ggsave("../figures/rmd/cloneDend3x.png", plot = cloneDend, height = 8, width = 35, units = "in", dpi = 300)


We removed the technical replicates/clones and re-ran ANGSD for all the following pop-gen analyses.

Dendrogram without clones

bams = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66,68,164,166,209,211),] # list of bams files and their populations (tech reps removed)

ma = as.matrix(read.table("../data/snps/sintNoClones3x.ibsMat")) # reads in IBS matrix produced by ANGSD
# ma = as.matrix(read.table("../data/snps/sintNoClones.ibsMat")) # reads in IBS matrix produced by ANGSD

dimnames(ma) = list(bams[,1],bams[,1])

pops = bams$site
depth = bams$depthZone

dend = ma %>% as.dist() %>% hclust(.,"ave") %>% as.dendrogram()
dData = dend %>% dendro_data()

# Making the branches hang shorter so we can easily see clonal groups
dData$segments$yend2 = dData$segments$yend
for(i in 1:nrow(dData$segments)) {
  if (dData$segments$yend2[i] == 0) {
    dData$segments$yend2[i] = (dData$segments$y[i] - 0.01)}}

dendPoints = dData$labels
dendPoints$pop = pops[order.dendrogram(dend)]
dendPoints$depth = depth[order.dendrogram(dend)]
rownames(dendPoints) = dendPoints$label

# Making points at the leaves to place symbols for populations
point = as.vector(NA)
for(i in 1:nrow(dData$segments)) {
  if (dData$segments$yend[i] == 0) {
    point[i] = dData$segments$y[i] - 0.01
  } else {
    point[i] = NA}}

dendPoints$y = point[!is.na(point)]

dendPoints$depth = factor(dendPoints$depth)
dendPoints$depth = factor(dendPoints$depth, levels(dendPoints$depth)[c(2,1)])

dendPoints$pop = factor(dendPoints$pop)
dendPoints$pop = factor(dendPoints$pop, levels(dendPoints$pop)[c(4, 1, 3, 2)])

dendNoCloneA = ggplot() +
  geom_segment(data = segment(dData), aes(x = x, y = y, xend = xend, yend = yend2), size = 0.5) +
  geom_point(data = dendPoints, aes(x = x, y = y, fill = pop, shape = depth), size = 4, stroke = 0.25) +
  scale_fill_manual(values = flPal, name= "Site:")+
  scale_shape_manual(values = c(21, 23), name = "Depth Zone:")+
 # spacing technical replicates further from leaf
  labs(y = "Genetic distance (1 - IBS)") +
  guides(fill = guide_legend(override.aes = list(shape = 22, size = 10), ncol = 2), shape = guide_legend(override.aes = list(size = 8), ncol = 1, order = 1)) +
  coord_cartesian(xlim = c(5, 218)) +
  theme_classic()

dendNoClone = dendNoCloneA + theme(
  axis.title.x = element_blank(),
  axis.text.x = element_blank(),
  axis.line.x = element_blank(),
  axis.ticks.x = element_blank(),
  axis.title.y = element_text(size = 24, color = "black", angle = 90),
  axis.text.y = element_text(size = 20, color = "black"),
  axis.line.y = element_line(),
  axis.ticks.y = element_line(),
  panel.grid = element_blank(),
  panel.border = element_blank(),
  panel.background = element_blank(),
  legend.key = element_blank(),
  legend.title = element_text(size = 24),
  legend.text = element_text(size = 20),
  legend.position = "bottom")

# dendNoClone


ma = as.matrix(read.table("../data/snps/sintFiltSnps.ibsMat")) # reads in IBS matrix produced by ANGSD

dimnames(ma) = list(pcadmix[,9],pcadmix[,9])

pops = pcadmix$pop
depth = pcadmix$depth
cluster = pcadmix$cluster

dend = ma %>% as.dist() %>% hclust(.,"ave") %>% as.dendrogram()
dData = dend %>% dendro_data()

# Making the branches hang shorter so we can easily see clonal groups
dData$segments$yend2 = dData$segments$yend
for(i in 1:nrow(dData$segments)) {
  if (dData$segments$yend2[i] == 0) {
    dData$segments$yend2[i] = (dData$segments$y[i] - 0.01)}}

dendPoints = dData$labels
dendPoints$pop = pops[order.dendrogram(dend)]
dendPoints$depth = depth[order.dendrogram(dend)]
dendPoints$cluster = cluster[order.dendrogram(dend)]
rownames(dendPoints) = dendPoints$label

# Making points at the leaves to place symbols for populations
point = as.vector(NA)
for(i in 1:nrow(dData$segments)) {
  if (dData$segments$yend[i] == 0) {
    point[i] = dData$segments$y[i] - 0.01
  } else {
    point[i] = NA}}

dendPoints$y = point[!is.na(point)]

dendPoints$depth = factor(dendPoints$depth)
dendPoints$depth = factor(dendPoints$depth, levels(dendPoints$depth)[c(2,1)])

dendPoints$pop = factor(dendPoints$pop)
dendPoints$pop = factor(dendPoints$pop, levels(dendPoints$pop)[c(4, 1, 3, 2)])

dendPoints$cluster = factor(dendPoints$cluster)

dendLA = ggplot() +
  geom_segment(data = segment(dData), aes(x = x, y = y, xend = xend, yend = yend2), size = 0.5) +
  geom_point(data = dendPoints, aes(x = x, y = y, fill = cluster, shape = depth), size = 4, stroke = 0.25) +
  scale_fill_manual(values = kColPal, name= "Lineage:")+
  scale_shape_manual(values = c(21, 23), name = "Depth Zone:")+
 # spacing technical replicates further from leaf
  labs(y = "Genetic distance (1 - IBS)") +
  guides(fill = guide_legend(override.aes = list(shape = 22, size = 10), ncol = 3), shape = guide_legend(override.aes = list(size = 8), ncol = 1, order = 1)) +
  coord_cartesian(xlim = c(5, 218)) +
  theme_classic()

dendL = dendLA + theme(
  axis.title.x = element_blank(),
  axis.text.x = element_blank(),
  axis.line.x = element_blank(),
  axis.ticks.x = element_blank(),
  axis.title.y = element_text(size = 24, color = "black", angle = 90),
  axis.text.y = element_text(size = 20, color = "black"),
  axis.line.y = element_line(),
  axis.ticks.y = element_line(),
  panel.grid = element_blank(),
  panel.border = element_blank(),
  panel.background = element_blank(),
  legend.key = element_blank(),
  legend.title = element_text(size = 24),
  legend.text = element_text(size = 20),
  legend.position = "bottom")

# dendL

Dendrogram plots

dendPlots = (cloneDend / dendNoClone / dendL) +
  plot_annotation(tag_levels = 'A') +
  plot_layout(guides = "collect") & 
  theme(plot.tag = element_text(size = 32),
        legend.position = "bottom")

ggsave("../figures/figureS1.png", plot = dendPlots, height = 19.5, width = 35, units = "in", dpi = 300)

ggsave("../figures/figureS1.svg", plot = dendPlots, height = 19.5, width = 35, units = "in", dpi = 300)


Population structure

PCA

cov = read.table("../data/snps/pcangsd/fkSintPcangsd3x.cov") %>% as.matrix()

pcAdmix = read.table("../data/snps/k/Clumpp3xK4.output") %>% dplyr::select(V6, V7, V8, V9)
pcAdmix %>% summarise(sum(V6),sum(V7), sum(V8), sum(V9)) 
##    sum(V6) sum(V7) sum(V8) sum(V9)
## 1 128.7501 43.4095 31.2726 16.5678
pcAdmix = pcAdmix %>% rename("cluster1" = "V6", "cluster2" = "V7", "cluster3" = "V8", "cluster4" = "V9") %>%dplyr::select(order(colnames(.)))
  
pcaEig = eigen(cov)
sintPcaVar = pcaEig$values/sum(pcaEig$values)*100
head(sintPcaVar)
## [1] 8.8597260 3.9773927 3.4518340 0.6623574 0.5049635 0.4945946
pcangsd = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "pop" = site, "depth" = depthZone, "depthm" = depthM)

pcangsd$popdepth = as.factor(paste(pcangsd$pop, pcangsd$depth, sep = " "))

pcangsd$popdepth = factor(pcangsd$popdepth, levels(pcangsd$popdepth)[c(4, 3, 6, 5, 2, 1, 8, 7)])

pcangsd$pop = factor(pcangsd$pop)
pcangsd$pop = factor(pcangsd$pop, levels(pcangsd$pop)[c( 4, 1, 3, 2)])

pcangsd$depth = factor(pcangsd$depth)
pcangsd$depth = factor(pcangsd$depth, levels(pcangsd$depth)[c(2, 1)])

pcangsd$PC1 = pcaEig$vectors[,1]
pcangsd$PC2 = pcaEig$vectors[,2]
pcangsd$PC3 = pcaEig$vectors[,3]
pcangsd$PC4 = pcaEig$vectors[,4]

pcangsdClust = pcAdmix %>% mutate(cluster = ifelse(cluster1 < 0.75 & cluster2 < 0.75  & cluster3 < 0.75 & cluster4 < 0.75, "NA", ifelse(cluster1 >=0.75, 1, ifelse(cluster2 >= 0.75, 2, ifelse(cluster3 >= 0.75, 3,ifelse(cluster4 >= 0.75, 4, 0))))))

# pcangsdClust$clusterX = as.vector(d_clust$classification)

pcangsd = pcangsd %>% mutate(pcangsdClust)

pcangsd$cluster = as.factor(pcangsd$cluster)
levels(pcangsd$cluster) = c("Blue", "Teal", "Green", "Yellow", "Admixed")

bamsClusters = pcangsd %>% dplyr::select(sample, cluster) %>% dplyr::arrange(sample) 
bamsSamples = read.delim("../data/snps/bamsNoClones", header = FALSE)
bamsClusters$sample = bamsSamples$V1

# bamsClusters = as.data.frame(bamsClusters)

write.table(x = bamsClusters, file = "../data/snps/bamsClusters", sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE)

pcangsd = merge(pcangsd, aggregate(cbind(mean.x = PC1, mean.y = PC2)~popdepth, pcangsd, mean), by="popdepth")
pcangsd %>% group_by(depth,cluster) %>% summarize(n = n())
## `summarise()` has grouped output by 'depth'. You can override using the `.groups`
## argument.
## # A tibble: 9 × 3
## # Groups:   depth [2]
##   depth      cluster     n
##   <fct>      <fct>   <int>
## 1 Shallow    Blue       50
## 2 Shallow    Teal       25
## 3 Shallow    Green      29
## 4 Shallow    Yellow     15
## 5 Shallow    Admixed     1
## 6 Mesophotic Blue       81
## 7 Mesophotic Teal       15
## 8 Mesophotic Green       2
## 9 Mesophotic Admixed     2

Plot PCA

pcaTheme = theme(axis.title.x = element_text(color = "black", size = 10),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.title.y = element_text(color = "black", size = 10),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank(),
        legend.position = "none",
        legend.title = element_text(size = 8),
        legend.text = element_text(size = 8),
        legend.key.size = unit(5, "pt"),
        panel.border = element_rect(color = "black", size = 1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

pcaPlot12SA = ggplot() +
  geom_hline(yintercept = 0, color = "gray90", size = 0.25) +
  geom_vline(xintercept = 0, color = "gray90", size = 0.25) +
  geom_point(data = pcangsd, aes(x = PC1, y = PC2, fill = pop, shape = depth, color = pop), stroke = 0, size = 2.5, alpha = 0.5, show.legend = FALSE) +
  geom_point(data = pcangsd, aes(x = mean.x, y = mean.y, fill = pop, shape = depth), color = "black", size = 2.75, alpha = 1, stroke = 0.25) +
  scale_shape_manual(values = c(21, 23), name = "Depth Zone") +
  scale_fill_manual(values = flPal, name = "Site") +
  scale_color_manual(values = flPal, name = "Site") +
  labs(x = paste0("PC 1 (", format(round(sintPcaVar[1], 1), nsmall = 1)," %)"), y = paste0("PC 2 (", format(round(sintPcaVar[2], 1), nsmall = 1), " %)")) +
  guides(shape = guide_legend(override.aes = list(size = 2, stroke = 0.25, alpha = NA), order = 2, ncol = 1), fill = guide_legend(override.aes = list(shape = 22, size = 2, fill = flPal, alpha = NA), order = 1, ncol = 1)) +
  theme_bw()

pcaPlot12S = pcaPlot12SA +
  pcaTheme +
  theme(legend.position = c(0.17, 0.23))

pcaPlot12S

pcaPlot12LA = ggplot() +
  geom_hline(yintercept = 0, color = "gray90", size = 0.5) +
  geom_vline(xintercept = 0, color = "gray90", size = 0.5) +
  geom_point(data = pcangsd, aes(x = PC1, y = PC2, fill = cluster, shape = depth), color = "black", size = 2, alpha = 1, show.legend = TRUE) +
  scale_shape_manual(values = c(21, 23), name = "Depth Zone") +
  scale_fill_manual(values = kColPal, name = "Lineage") +
  labs(x = paste0("PC 1 (", format(round(sintPcaVar[1], 1), nsmall = 1)," %)"), y = paste0("PC 2 (", format(round(sintPcaVar[2], 1), nsmall = 1), " %)")) +
  guides(shape = "none", fill = guide_legend(override.aes = list(shape = 22, size = 2, fill = kColPal, alpha = NA), order = 1, ncol = 1))+
  theme_bw()

pcaPlot12L = pcaPlot12LA +
  pcaTheme +
  theme(legend.position = c(0.12,0.15))

pcaPlot23LA = ggplot() +
  geom_hline(yintercept = 0, color = "gray90", size = 0.5) +
  geom_vline(xintercept = 0, color = "gray90", size = 0.5) +
  geom_point(data = pcangsd, aes(x = PC3, y = PC2, fill = cluster, shape = depth), color = "black", size = 2, alpha = 1, show.legend = TRUE) +
  scale_shape_manual(values = c(21, 23), name = "Depth Zone") +
  scale_fill_manual(values = kColPal, name = "Lineage") +
  labs(x = paste0("PC 3 (", format(round(sintPcaVar[3], 1), nsmall = 1)," %)"), y = paste0("PC 2 (", format(round(sintPcaVar[2], 1), nsmall = 1), " %)")) +
  guides(shape = guide_legend(override.aes = list(size = 2, stroke = 0.5, alpha = NA), order = 2, ncol = 1), fill = guide_legend(override.aes = list(shape = 22, size = 2, fill = kColPal, alpha = NA), order = 1, ncol = 1, byrow = TRUE))+
  theme_bw()

pcaPlot23L = pcaPlot23LA +
  pcaTheme

Put all plots together

pcaPlots = ((pcaPlot12S + theme(axis.title.y = element_text(margin = ggplot2::margin(r = -20, unit = "pt")))) | pcaPlot12L | pcaPlot23L) +
  plot_annotation(tag_levels = 'A') &
  theme(plot.tag = element_text(size = 18),
        panel.background = element_rect(fill = "white"),
        legend.spacing = unit(-5, "pt"),
        legend.key = element_blank(),
        legend.background = element_blank())

pcaPlots

Admixture

Prepare admixture outputs for plotting

fkSintAdmix = pcangsd %>%dplyr::select(-PC1, -PC2, -PC3, -PC4, -cluster, -depthm, -mean.x, -mean.y)
fkSintAdmix$pop = factor(fkSintAdmix$pop, levels(fkSintAdmix$pop)[c( 4, 3, 2, 1)])

fkSintAdmix = arrange(fkSintAdmix, pop, depth, -cluster1, -cluster2, cluster4)
popCounts = fkSintAdmix %>% group_by(pop, depth) %>% summarise(n = n())
## `summarise()` has grouped output by 'pop'. You can override using the `.groups`
## argument.
popCounts
## # A tibble: 8 × 3
## # Groups:   pop [4]
##   pop           depth          n
##   <fct>         <fct>      <int>
## 1 Riley's Hump  Shallow       30
## 2 Riley's Hump  Mesophotic    15
## 3 Tortugas Bank Shallow       30
## 4 Tortugas Bank Mesophotic    25
## 5 Lower Keys    Shallow       30
## 6 Lower Keys    Mesophotic    30
## 7 Upper Keys    Shallow       30
## 8 Upper Keys    Mesophotic    30
popCountList = reshape2::melt(lapply(popCounts$n,function(x){c(1:x)}))
fkSintAdmix$ord = popCountList$value

fkSintAdmixMelt = melt(fkSintAdmix, id.vars=c("sample", "pop", "depth", "popdepth", "ord"), variable.name="Ancestry", value.name="Fraction")

fkSintAdmixMelt$Ancestry = factor(fkSintAdmixMelt$Ancestry)
fkSintAdmixMelt$Ancestry = factor(fkSintAdmixMelt$Ancestry, levels = rev(levels(fkSintAdmixMelt$Ancestry)))

popAnno = data.frame(x1 = c(0.5, 0.5, 0.5, 0.5), x2 = c(30.5, 30.5, 30.5, 30.5),
                     y1 = -0.1, y2 = -0.1, sample = NA, Ancestry = NA, depth = "Mesophotic", 
                     ord  = NA, Fraction = NA,
                     pop = c("Riley's Hump", "Tortugas Bank", 
                                  "Lower Keys", "Upper Keys"))
popAnno$pop = factor(popAnno$pop)
popAnno$pop = factor(popAnno$pop, levels = levels(popAnno$pop)[c(4, 1, 3, 2)])

Make admixture plots

admixPlotA = ggplot(data = fkSintAdmixMelt, aes(x = ord, y = Fraction, fill = Ancestry, order = sample)) +
  geom_segment(data = popAnno, aes(x = x1, xend = x2, y = -.12, yend = -.12, color = pop), size = 7) +
  geom_bar(stat = "identity", position = "fill", width = 1, colour = "grey25", size = 0.2) +
  facet_grid(factor(depth) ~ pop, switch = "both") +
  geom_text(data = (fkSintAdmixMelt %>% filter(depth == "Mesophotic", pop %in% c("Riley's Hump", "Tortugas Bank"), sample %in% c(   
"SFK001", "SFK100"), Ancestry == "cluster1")), x = 15.5, y = -.1, aes(label = pop), size = 4, color = "#FFFFFF") +
  geom_text(data = (fkSintAdmixMelt %>% filter(depth == "Mesophotic", pop %in% c("Lower Keys", "Upper Keys"), sample %in% c(    
"SFK101", "SFK201"), Ancestry == "cluster1")), x = 15.5, y = -.1, aes(label = pop), size = 3.5, color = "#000000") +
  
  scale_fill_manual(values = kColPal) +
  scale_color_manual(values = flPal) +
  scale_x_discrete(expand = c(0.005, 0.005)) +
  scale_y_continuous(expand = c(0.001, 0.001)) +
  coord_cartesian(ylim = c(0.0, 1.0), clip = "off") +
theme_bw()
  
admixPlot = admixPlotA + 
  theme_bw()+
  theme(
  panel.grid = element_blank(),
  panel.background = element_rect(fill = "gray70"),
  plot.background = element_blank(),
  panel.border = element_rect(fill = NA, color = "black", size = 0.75, linetype = "solid"),
  panel.spacing.x = grid:::unit(0.05, "lines"),
  panel.spacing.y = grid:::unit(0.05, "lines"),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks.x = element_blank(),
  axis.ticks.y = element_blank(),
  axis.title = element_blank(),
  strip.background.x = element_blank(),
  strip.background.y = element_blank(),
  strip.text = element_text(size = 8),
  strip.text.y.left = element_text(size = 10, angle = 90),
  strip.text.x.bottom = element_text(vjust = 1, color = NA),
  legend.key = element_blank(),
  legend.position = "none",
  legend.title = element_text(size = 8))



admixPlot

Lineage demographics

Depth distribution of lineages

leveneTest(lm(depthm ~ cluster, data = subset(pcangsd, subset = pcangsd$cluster!="Admixed")))
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value   Pr(>F)    
## group   3  6.9537 0.000174 ***
##       213                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
depthAov = welch_anova_test(depthm ~ cluster, data = subset(pcangsd, subset = pcangsd$cluster!="Admixed"))

dF = depthAov$statistic[[1]]

depthPH = games_howell_test(depthm ~ cluster, data = subset(pcangsd, subset = pcangsd$cluster!="Admixed"), conf.level = 0.95) %>% as.data.frame()

depthLetters = data.frame(x = factor(c("Blue", "Teal", "Green", "Yellow")), y = c(2.5, 2.5, 2.5, 2.5),  grp = c("a", "b", "bc", "c"))

lineageViolinA = ggplot(data = subset(pcangsd, subset = pcangsd$cluster!="Admixed"), aes(x = cluster, y = depthm)) +
  annotate(geom = "rect", xmin = -Inf, xmax = Inf, ymin = 30, ymax = Inf,  fill = "black", alpha = 0.15, color = NA) +
  geom_beeswarm(aes(fill = cluster), shape = 21, size = 2, cex = 1.5, alpha = 0.5) +
  geom_violin(aes(fill = cluster),adjust = 1, linewidth = 0, color = "black", alpha = 0.35, width = 0.9, trim = F, scale = "width") +
  geom_violin(adjust = 1, linewidth = 0.4, color = "black", alpha = 1, width = 0.9, trim = F, fill = NA, scale = "width") +
  geom_boxplot(width = 0.2, color = "black", fill = "white", outlier.colour = NA, linewidth = 0.6, alpha = 0.5) +
  geom_text(data = depthLetters, aes(x = x, y = y, label = grp), size = 4) +
  annotate(geom = "text", x = 3.75, y =50, label = bquote(italic("F")~" = "~.(dF)*"; "~italic("p")~" < 0.001"), size = 3) +
  scale_fill_discrete(type = kColPal, name = "Lineage") +
  scale_color_discrete(type = kColPal, name = "Lineage") +
  xlab("Lineage") +
  ylab("Depth (m)") +
  scale_y_reverse(breaks = seq(5, 50, 5)) +
  theme_bw()

lineageViolin = lineageViolinA + theme(
        axis.title = element_text(color = "black", size = 12),
        axis.text = element_text(color = "black", size = 10),
        legend.position = "none",
        legend.key.size = unit(0.3, 'cm'),
        panel.border = element_rect(color = "black", size = 1),
        panel.background = element_blank(),
        plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

lineageViolin

Lineage differentiation

Measuring with global weighted FST calculated from SFS

First prepare and sort FST for plotting

pop.order = c("Blue", "Teal", "Green", "Yellow")

# reads in fst 
fstMa1 <- read.delim("../data/snps/sintKFst3x.out") %>% dplyr::select(-fst) %>% df_to_pw_mat(., "pop1", "pop2", "weightedFst")

fstMa1
##            Blue     Teal    Green   Yellow
## Blue   0.000000 0.047964 0.185205 0.162474
## Teal   0.047964 0.000000 0.210652 0.184036
## Green  0.185205 0.210652 0.000000 0.209234
## Yellow 0.162474 0.184036 0.209234 0.000000
fstMa = fstMa1

upperTriangle(fstMa, byrow = TRUE) <- lowerTriangle(fstMa)
fstMa <- fstMa[,pop.order] %>%
  .[pop.order,]
fstMa[upper.tri(fstMa)] <- NA
fstMa <- as.data.frame(fstMa)

# rearrange and reformat matrix
fstMa$Pop = factor(row.names(fstMa), levels = unique(pop.order))



# melt matrix data (turn from matrix into long dataframe)
fst = melt(fstMa, id.vars = "Pop", value.name = "Fst", variable.name = "Pop2", na.rm = FALSE)

fst$Fst = round(fst$Fst, 3)

fst$site = fst$Pop
fst$site = factor(gsub("\\n.*", "", fst$site))
fst$site = factor(fst$site, levels = levels(fst$site)[c(1, 3, 2, 4)])

fst$site2 = fst$Pop2
fst$site2 = factor(gsub("\\n.*", "", fst$site2))
fst$site2 = factor(fst$site2, levels = levels(fst$site2)[c(1, 3, 2, 4)])

fst$Pop2 = factor(fst$Pop2, levels = levels(fst$Pop2)[c(4, 3, 2, 1)])

Construct a heatmap of FST values

fstHeatmapA = ggplot(data = fst %>% filter(Fst != 0), aes(Pop, Pop2, fill = as.numeric(as.character(Fst)))) +
  geom_tile(color = "white") +
  geom_segment(data = fst, aes(x = 0.475, xend = 0.15, y = Pop2, yend = Pop2, color = site2), size = 21.25) + #colored boxes for Y-axis labels
  geom_segment(data = fst, aes(x = Pop, xend = Pop, y = 0.2, yend = 0.475, color = site), size = 25) + #colored boxes for X-axis labels
  scale_color_manual(values = kColPal, guide = NULL) +
  # scale_fill_gradient(low = "gray95", high = "gray35", limit = c(0, 0.22), space = "Lab", name = expression(paste(italic("F")[ST])), na.value = NA,  guide = "colourbar") +
  # scale_fill_gradientn(colours = c("white", paletteer_c("viridis::mako", n = 20)[13:1]), limit = c(0,0.25), space = "Lab", name = expression(paste(italic("F")["ST"]), na.value = "transparent")) +
  scale_fill_gradientn(colours = paletteer_c("viridis::mako", n = 10, direction = -1)[c(1:7)], limit = c(0, 0.22), space = "Lab", name = expression(paste(italic("F")["ST"]), na.value = "transparent")) +
  # scale_fill_paletteer_c(`"viridis::mako"`, direction = -1) +
  geom_text(data = fst %>% filter(Fst != 0), aes(Pop, Pop2, label = Fst), color = "black", size = 3.5, fontface = "bold") +
  guides(fill = guide_colorbar(barwidth = 7.5, barheight = 0.75, title.position = "top", title.hjust = 0.5, direction = "horizontal", ticks.colour = "black", frame.colour = "black")) +
  scale_y_discrete(position = "left", limits = rev(levels(fst$Pop2))) +
  scale_x_discrete(limits = levels(fst$Pop2)[c(1:4)]) +
  coord_cartesian(xlim = c(1, 4), ylim = c(1, 4), clip = "off") +
  theme_minimal()

fstHeatmap = fstHeatmapA + theme(
  axis.text.x = element_text(vjust = 3.5, size = 10, hjust = 0.5, color = "black"),
  axis.text.y = element_text(size = 10, color = "black", angle = 90, hjust = 0.5, vjust = -1.5),
  axis.title.x = element_blank(),
  axis.title.y = element_blank(),
  panel.grid.major = element_blank(),
  panel.border = element_blank(),
  axis.ticks = element_blank(),
  legend.title = element_text(size = 8, color = "black"),
  legend.text = element_text(size = 8, color = "black"),
  legend.position = c(0.6, 0.9),
  plot.background = element_blank(),
  panel.background = element_blank(),
)

fstHeatmap

Lineage demographics through time

Making stairway plot of effective population sizes of each lineage throughout time

bl = read.table("../data/snps/sintBlue.final.summary", header = TRUE) %>% mutate("Lineage" = "Blue")
tl = read.table("../data/snps/sintTeal.final.summary", header = TRUE) %>% mutate("Lineage" = "Teal")
gn = read.table("../data/snps/sintGreen.final.summary", header = TRUE) %>% mutate("Lineage" = "Green")
yl = read.table("../data/snps/sintYellow.final.summary", header = TRUE) %>% mutate("Lineage" = "Yellow")

swData = rbind(bl, tl, gn, yl)
swData$Lineage = factor(swData$Lineage)
swData$Lineage = factor(swData$Lineage, levels = levels(swData$Lineage)[c(1,3,2,4)])

Constuct plot

swPlotA = ggplot(data = swData, aes(x = year, y = Ne_median, ymin = Ne_12.5., ymax = Ne_87.5., color = Lineage, fill = Lineage)) +
  geom_ribbon(color = NA, aes(alpha = Lineage)) +
  # geom_line(size = 0.6) +
  geom_line(linewidth = 1.15) +
  scale_fill_manual(values = kColPal[c(1:4)]) +
  scale_color_manual(values = kColPal[c(1:4)]) +
  scale_alpha_manual(values = c(0.25, 0.25, 0.35, 0.4)) +
  scale_x_continuous(name = "KYA", limits = c(0,5.25e5), breaks = c(1e5,2e5,3e5,4e5,5e5), labels = c("100","200", "300", "400", "500")) +
  scale_y_continuous(name = bquote(italic(N[e])~"(x10"^"3"*")"), limits = c(0,14e5), breaks = c(2.5e5,5e5,7.5e5,10e5,12.5e5), labels = c("250","500", "750", "1000", "1250"))+

  coord_cartesian(xlim = c(5.25e5, 0), expand = FALSE) +
  theme_bw()

swPlot = swPlotA + theme(
    axis.title = element_text(color = "black", size = 12),
    axis.text = element_text(color = "black", size = 10),
    legend.key.size = unit(0.3, 'cm'),
    legend.title = element_text(color = "black", size = 12),
    legend.text = element_text(color = "black", size = 12),
    legend.position = "none",
    # legend.position = c(0.85, 0.82),
    plot.background = element_blank(),
    panel.background = element_blank(),
    panel.border = element_rect(size = 1),
    panel.grid = element_blank()
)

swPlot


Heterozygosity and Inbreeding

popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "Site" = site, "Depth" = depthZone, "depthm" = depthM) # Reads in population data
popData$a = c(0:219)

popData$Site = factor(popData$Site)
popData$Site = factor(popData$Site, levels = levels(popData$Site)[c(2,3,1,4)]) 
popData$Depth = factor(popData$Depth)
popData$Depth = factor(popData$Depth, levels = levels(popData$Depth)[c(2,1)]) 

sampleData = fknmsSites[-c(66,68,164,166,209,211),] %>% group_by(site, depthZone)%>% summarise(depthZone = (first(depthZone)), depthRange = paste(min(depthM), "--", max(depthM), sep = ""), meanDepth = round(mean(depthM),1), n = n())%>% droplevels() %>% as.data.frame()
## `summarise()` has grouped output by 'site'. You can override using the `.groups`
## argument.
# Average depth of populations
fkPopDepths = fknmsSites[-c(66,68,164,166,209,211),] %>%  group_by(site, depthZone) %>% summarise(avgDepthM = mean(depthM), n = n())
## `summarise()` has grouped output by 'site'. You can override using the `.groups`
## argument.
fkPopDepths
## # A tibble: 8 × 4
## # Groups:   site [4]
##   site          depthZone  avgDepthM     n
##   <fct>         <fct>          <dbl> <int>
## 1 Upper Keys    Shallow         23.6    30
## 2 Upper Keys    Mesophotic      43.8    30
## 3 Lower Keys    Shallow         18.0    30
## 4 Lower Keys    Mesophotic      32.8    30
## 5 Tortugas Bank Shallow         21.1    30
## 6 Tortugas Bank Mesophotic      32.0    25
## 7 Riley's Hump  Shallow         26.4    30
## 8 Riley's Hump  Mesophotic      33.2    15
sampleTab = sampleData
colnames(sampleTab) = c("Site", "Depth zone", "Sampling \ndepth (m)", "Average \ndepth (m)", "n")

sampleTab$Site
## [1] Upper Keys    Upper Keys    Lower Keys    Lower Keys    Tortugas Bank Tortugas Bank
## [7] Riley's Hump  Riley's Hump 
## Levels: Upper Keys Lower Keys Tortugas Bank Riley's Hump
finalTabSite = c("Upper Keys", "", "Lower Keys","", "Tortugas Bank", "", "Riley's Hump", "")

sampleTab$Site = finalTabSite

hetAll = read.table("../data/snps/sintHet3x") 
colnames(hetAll) = c("sample", "He")
hetAll$sample = str_pad(hetAll$sample, 3, pad = "0")
hetAll$sample = paste("SFK",hetAll$sample, sep ="")

sintBreed = read.delim("../data/snps/fkSintF.indF", header = FALSE)

sintRelate = read.delim("../data/snps/fkSintFiltRelate3x")
sintRelate2 = sintRelate %>% group_by(a, b) %>% dplyr::select("Rab" = rab, "theta" = theta)
## Adding missing grouping variables: `a`, `b`
sintRelate2 = sintRelate2 %>% left_join(popData, by = "a") %>% left_join(popData, by = c("b" = "a"), suffix = c(".a", ".b")) 

sintRelate2$popDepth.a = paste(sintRelate2$Site.a, sintRelate2$Depth.a, sep = " ")
sintRelate2$popDepth.b = paste(sintRelate2$Site.b, sintRelate2$Depth.b, sep = " ")

sintRelate2 = sintRelate2 %>% left_join((pcangsd %>%dplyr::select(sample, cluster)) , by = c("sample.a" = "sample")) %>% left_join((pcangsd %>%dplyr::select(sample, cluster)) , by = c("sample.b" = "sample"))

sintRelate = sintRelate2 %>% filter(cluster.x != "Admixed",cluster.x == cluster.y) %>% rename(Depth = Depth.a, Site = Site.a, cluster = cluster.x)

sintRelateMean = sintRelate %>% group_by(Site, Depth) %>% dplyr::summarize(N = n(), meanRab = mean(Rab), seRab = sd(Rab)/sqrt(N), meanTheta = mean(theta), seTheta = sd(theta)/sqrt(N)) %>% dplyr::select(-N)
## `summarise()` has grouped output by 'Site'. You can override using the `.groups`
## argument.
het = left_join(popData, hetAll, by = "sample") %>% mutate("inbreed" = sintBreed$V1) %>% left_join((pcangsd %>% dplyr::select(sample, cluster)) , by = "sample") %>% dplyr::select(-a)

hetStats = het %>% group_by(Site, Depth) %>% summarise(N = n(), meanAll = mean(He), sdAll = sd(He), seAll = sd(He)/sqrt(N), meanInbreed = mean(inbreed), sdInbreed = sd(inbreed), seInbreed = sd(inbreed)/sqrt(N)) %>% left_join(sintRelateMean)
## `summarise()` has grouped output by 'Site'. You can override using the `.groups`
## argument.
## Joining with `by = join_by(Site, Depth)`
min(hetStats$meanAll, na.rm = TRUE)
## [1] 0.002376667
max(hetStats$meanAll, na.rm = TRUE)
## [1] 0.002664
hetTab = hetStats %>% arrange(desc(Site))

hetTab$n = hetTab$N
hetTab$Ha = paste(round(hetTab$meanAll, 4), "±", round(hetTab$seAll, 5), sep = " ")
hetTab$F =  paste(round(hetTab$meanInbreed, 2), "±", round(hetTab$seInbreed, 3), sep = " ")
hetTab$Rab =  paste(round(hetTab$meanRab, 2), "±", round(hetTab$seRab, 3), sep = " ")
hetTab$Theta =  paste(round(hetTab$meanTheta, 2), "±", round(hetTab$seTheta, 4), sep = " ")

hetTab$`Sampling \ndepth (m)` = sampleTab$`Sampling \ndepth (m)`
hetTab$`Average \ndepth (m)` = sampleTab$`Average \ndepth (m)`

hetTab = hetTab %>% dplyr::select(Site, Depth, `Sampling \ndepth (m)`, `Average \ndepth (m)`, n, Ha, F, Rab, Theta)
colnames(hetTab)[2] = "Depth \nzone"

finalTabSite = c("Upper Keys", "", "Lower Keys", "", "Tortugas Bank", "", "Riley's Hump", "")

hetTab$Site = finalTabSite

hetTabPub = hetTab %>% dplyr::select(-Theta) %>% 
  flextable() %>%
  flextable::compose(part = "header", j = "n", value = as_paragraph(as_i("n"))) %>%
   flextable::compose(part = "header", j = "Ha", value = as_paragraph(as_i("H"), as_sub("A"))) %>%
  flextable::compose(part = "header", j = "F", value = as_paragraph(as_i("F"))) %>%
  flextable::compose(part = "header", j = "Rab", value = as_paragraph(as_i("R"), as_i(as_sub("AB")))) %>%
  flextable::font(fontname = "Times New Roman", part = "all") %>%
  flextable::fontsize(size = 10, part = "all") %>%
  flextable::bold(part = "header") %>%
  flextable::align(align = "left", part = "all") %>%
  flextable::autofit()

table2 = read_docx()
table2 = body_add_flextable(table2, value = hetTabPub)
print(table2, target = "../tables/table2.docx")

hetTabPub

Heterozygosity, diversity, and inbreeding plots

Heterozygosity across all RAD loci by lineage

leveneTest(lm(He ~ cluster, data = subset(het, subset = pcangsd$cluster!="Admixed")))
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value             Pr(>F)    
## group   4  18.093 0.0000000000008437 ***
##       212                               
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
hetAov = welch_anova_test(He ~ cluster, data = subset(het, subset = het$cluster!="Admixed"))

hF = hetAov$statistic[[1]]

hetPH = games_howell_test(He ~ cluster, data = subset(het, subset = het$cluster!="Admixed"), conf.level = 0.95) %>% as.data.frame()


# hetLetters = data.frame(x = factor(c("Blue", "Teal", "Green", "Yellow")), y = c(0.0039, 0.0039, 0.0039, 0.0039),  grp = c("a", "bc", "b", "c"))

hetLetters = data.frame(x = factor(c("Blue", "Teal", "Green", "Yellow")), y = c(0.0039, 0.0039, 0.0039, 0.0039),  grp = c("a", "b", "c", "b"))

hetPlotKA = ggplot(data = het %>% filter(cluster != "Admixed"), aes(x = cluster, y = He)) +
  geom_beeswarm(aes(fill = cluster), shape = 21, size = 2, cex = 0.75, alpha = 0.5) + 
  geom_violin(aes(fill = cluster, group = cluster), adjust = 1, linewidth = 0, color = "black", alpha = 0.35, width = 0.9, trim = F, scale = "width") +
  geom_violin(aes(fill = cluster, group = cluster), adjust = 1, linewidth = 0.4, color = "black", alpha = 1, width = 0.9, trim = F, fill = NA, scale = "width") +
  geom_boxplot(aes(fill = cluster, group = cluster), width = 0.2, color = "black", fill = "white", outlier.colour = NA, linewidth = 0.6, alpha = 0.5) +  xlab("Lineage") +
  geom_text(data = hetLetters, aes(x = x, y = y, label = grp), size = 4) +
   annotate(geom = "text", x = 3.65, y =0.0036, label = bquote(italic("F")~" = "~.(hF)*"; "~italic("p")~" < 0.001"), size = 3) +
  scale_fill_discrete(type = kColPal, name = "Lineage") +
  xlab("Lineage") +
  ylab("Heterozygosity") +
  scale_y_continuous(breaks = seq(0.0022, 0.0038, 0.0004)) +
  coord_cartesian(expand = TRUE, xlim = c(0.85, 4)) +
  theme_bw()

 hetPlotK = hetPlotKA + theme(
        axis.title = element_text(color = "black", size = 12),
        axis.text = element_text(color = "black", size = 10),
        legend.position = "none",
        legend.key.size = unit(0.3, 'cm'),
        panel.border = element_rect(color = "black", size = 1),
        panel.background = element_blank(),
        plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

# hetPlotK

Mean inbreeding plot

leveneTest(lm(inbreed ~ cluster, data = subset(het, subset = pcangsd$cluster!="Admixed")))
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value     Pr(>F)    
## group   4  6.3025 0.00008245 ***
##       212                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ibAov = welch_anova_test(inbreed ~ cluster, data = subset(het, subset = het$cluster!="Admixed"))

iF = ibAov$statistic[[1]]

ibPH = games_howell_test(inbreed ~ cluster, data = subset(het, subset = het$cluster!="Admixed"), conf.level = 0.95) %>% as.data.frame()


inbreedLetters = data.frame(x = factor(c("Blue", "Teal", "Green", "Yellow")), y = c(0.38, 0.38, 0.38, 0.38),  grp = c("a", "b", "c", "c"))

inbreedingPlot = ggplot(data = het %>% filter(cluster!="Admixed"), aes(x = cluster, y = inbreed)) +
  geom_beeswarm(aes(fill = cluster), shape = 21, size = 2, cex = 1.5, alpha = 0.5) +
  geom_violin(aes(fill = cluster), adjust = 1, linewidth = 0, color = "black", alpha = 0.35, width = 0.9, trim = F, scale = "width") +
  geom_violin(adjust = 1, linewidth = 0.4, color = "black", alpha = 1, width = 0.9, trim = F, fill = NA, scale = "width") +
  geom_boxplot(aes(fill = cluster),width = 0.2, color = "black", fill = "white", outlier.colour = NA, linewidth = 0.6, alpha = 0.5) + 
  geom_text(data = inbreedLetters, aes(x = x, y = y, label = grp), size = 4) +
  annotate(geom = "text", x = 3.6, y =0.032, label = bquote(italic("F")~" = "~.(iF)*"; "~italic("p")~" < 0.001"), size = 3) +
  xlab("Lineage") +
  ylab(bquote(~"Inbreeding coefficient ("*italic(F)*")")) +
  scale_fill_manual(values = kColPal) +  
  scale_color_manual(values = kColPal) +
  scale_y_continuous(breaks=seq(0, 0.4, by = .05)) +
  coord_cartesian(expand = TRUE, xlim = c(0.78, 4)) +
  theme_bw() +
  theme(legend.position = "none",
        axis.text = element_text(size = 10, color = "black"),
        axis.title = element_text(size = 12, color = "black"),
        panel.border = element_rect(color = "black", size = 1),
        plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

inbreedingPlot

Nucleotide diversity plot

# mean depth for lineages
subset(pcangsd, subset = pcangsd$cluster!="Admixed") %>% group_by(cluster) %>% summarize(depth = mean(depthm))
## # A tibble: 4 × 2
##   cluster depth
##   <fct>   <dbl>
## 1 Blue     31.4
## 2 Teal     26.3
## 3 Green    22.5
## 4 Yellow   20.2
npgList = list(read_tsv("../data/snps/blue3x.thetas.idx.pestPG") %>% mutate(lineage = "Blue", depth = 31.4),
               read_tsv("../data/snps/teal3x.thetas.idx.pestPG") %>% mutate(lineage = "Teal", depth = 26.3),
               read_tsv("../data/snps/green3x.thetas.idx.pestPG") %>% mutate(lineage = "Green", depth = 22.5),
               read_tsv("../data/snps/yellow3x.thetas.idx.pestPG")%>% mutate(lineage = "Yellow", depth = 20.2))
## Rows: 30 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr  (2): #(indexStart,indexStop)(firstPos_withData,lastPos_withData)(WinStart,WinSt...
## dbl (12): WinCenter, tW, tP, tF, tH, tL, Tajima, fuf, fud, fayh, zeng, nSites
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 30 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr  (2): #(indexStart,indexStop)(firstPos_withData,lastPos_withData)(WinStart,WinSt...
## dbl (12): WinCenter, tW, tP, tF, tH, tL, Tajima, fuf, fud, fayh, zeng, nSites
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 30 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr  (2): #(indexStart,indexStop)(firstPos_withData,lastPos_withData)(WinStart,WinSt...
## dbl (12): WinCenter, tW, tP, tF, tH, tL, Tajima, fuf, fud, fayh, zeng, nSites
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 30 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr  (2): #(indexStart,indexStop)(firstPos_withData,lastPos_withData)(WinStart,WinSt...
## dbl (12): WinCenter, tW, tP, tF, tH, tL, Tajima, fuf, fud, fayh, zeng, nSites
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
piAll = purrr::reduce(npgList, rbind) %>% 
  group_by(lineage) %>% 
  mutate(tPps = tP/nSites) %>%
  summarize(tPps = mean(tPps), depth = min(depth))

piAll$lineage = as.factor(piAll$lineage)
piAll$lineage = factor(piAll$lineage, levels(piAll$lineage)[c(1, 3, 2, 4)])

lmpi = lm(tPps~depth, data=piAll)
summary(lmpi)
## 
## Call:
## lm(formula = tPps ~ depth, data = piAll)
## 
## Residuals:
##           1           2           3           4 
##  0.00009393 -0.00028006 -0.00006686  0.00025299 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 0.00355804 0.00083771   4.247   0.0512 .
## depth       0.00006938 0.00003291   2.108   0.1696  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.000279 on 2 degrees of freedom
## Multiple R-squared:  0.6896, Adjusted R-squared:  0.5345 
## F-statistic: 4.444 on 1 and 2 DF,  p-value: 0.1696
r2 = round(summary(lmpi)$r.squared, 3)

nuclDivPlot = ggplot(piAll, aes(x = depth, y = tPps)) +
  geom_smooth(se = FALSE, color = 'black', method='lm', linewidth = 0.75) +
  geom_point(aes(fill = lineage),shape = 21, size = 3) +
  scale_color_manual(values = kColPal) +
  scale_fill_manual(values = kColPal) +
  labs(x='Depth (m)', y = bquote("Nucleotide diversity ("*pi*")"), shape = 'Lineage') +
  annotate(geom = "text", x = 30, y = 0.0048, label = bquote(italic(R^2)~"="~.(r2)), size = 3) + 
  theme_bw() +
  theme(axis.title.y = element_text(color = "black", size = 12),
        axis.title.x = element_text(color = "black", size = 12),
        axis.text = element_text(color = "black", size = 10),
        legend.position = "none",
        legend.key.size = unit(0.3, 'cm'),
        panel.border = element_rect(color = "black", size = 1),
        panel.background = element_blank(),
        plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

nuclDivPlot
## `geom_smooth()` using formula = 'y ~ x'

Lineage plots

lineagePlots = (lineageViolin | hetPlotK | inbreedingPlot) / (nuclDivPlot | swPlot | fstHeatmap) +
  plot_annotation(tag_levels = "A") &
  theme(plot.tag = element_text(size = 14))

# lineagePlots

ggsave("../figures/figure3.png", plot = lineagePlots, height = 7, width = 12, units = "in", dpi = 300)
## `geom_smooth()` using formula = 'y ~ x'
ggsave("../figures/figure3.svg", plot = lineagePlots, height = 7, width = 12, units = "in", dpi = 300)
## `geom_smooth()` using formula = 'y ~ x'

Outliers

#average depths
depthsBayeScan = pcadmix %>% filter(cluster != "Admixed") %>% dplyr::mutate(linDepth = paste(cluster, depth, sep = "_")) %>% group_by(linDepth) %>% summarize(meanDepth = mean(depthm))

depthsBayeScan
## # A tibble: 7 × 2
##   linDepth         meanDepth
##   <chr>                <dbl>
## 1 Blue_Mesophotic       36.2
## 2 Blue_Shallow          23.7
## 3 Green_Mesophotic      31.4
## 4 Green_Shallow         21.9
## 5 Teal_Mesophotic       35.2
## 6 Teal_Shallow          21.0
## 7 Yellow_Shallow        20.2
bayescan = read.table("../data/snps/fkSint3x.baye_fst.txt",header=T) %>% mutate(loc = rownames(.), out.05 = ifelse(qval < 0.05, 1, 0), out.1 = ifelse(qval < 0.1, 1, 0))
bayescan[bayescan[, 3]<=0.0001, 3] = 0.0001

bayeScEnv = read.table("../data/snps/fkSint3x.bayeS_fst.txt", header=T) %>% filter(qval_g < 0.05) %>% mutate(loc = rownames(.), depthOut = 1) %>% dplyr::select(loc, depthOut)

bayescan = bayescan %>% left_join(bayeScEnv) 
## Joining with `by = join_by(loc)`
bayescan$depthOut = bayescan$depthOut %>% replace_na(0)

sum(bayescan$out.05)
## [1] 120
sum(bayescan$out.1)
## [1] 157
sum(bayescan$depthOut)
## [1] 0
for(i in 1:nrow(bayescan)){
  if(bayescan$depthOut[i] == 1){
    bayescan$out.05[i] = 2
  }
}

bayescanPlotA = ggplot(data = bayescan, aes(x = log10(qval), y = fst, color = as.factor(out.05), alpha = as.factor(out.05))) +  
  geom_point(size = 1) +
  geom_vline(xintercept = log10(0.05), linetype = 2, color = purple) +
  xlab(expression(log[10]*"("*italic("q")*"-value)")) +
  ylab(expression(italic("F")[ST])) +
  scale_x_reverse() +
  scale_color_manual(values = c("grey45", purple, pink)) +
  scale_alpha_manual(values = c(0.25, 0.25, 0.5)) +
  theme_bw()

bayescanPlot = bayescanPlotA +
        theme(axis.title.x = element_text(color = "black", size = 12),
        axis.text.x = element_text(color = "black", size = 12),
        axis.ticks.x = element_line(color = "black"),
        axis.title.y = element_text(color = "black", size = 12),
        axis.text.y = element_text(color = "black", size = 10),
        axis.ticks.y = element_line(color = "black"),
        legend.position = "none",
        legend.key.size = unit(0.3, 'cm'),
        panel.border = element_rect(color = "black"),
        panel.background = element_rect(fill = "white"),
        plot.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

# bayescanPlot

ggsave("../figures/figureS2.svg", plot = bayescanPlot, width = 14, height = 8, units = "cm", dpi = 300)
ggsave("../figures/figureS2.png", plot = bayescanPlot, width = 14, height = 8, units = "cm", dpi = 300)

Isolation by distance and redundancy analysis

S. intersepta isolation by distance

Mantel’s test

# Isolation by distance
library(geosphere)

#Get the geographic distances in km
coords  = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% 
dplyr::select(longDD, latDD)

dGeo = as.dist((distm(coords, fun = distGeo)/1000), diag = TRUE)
snpDist = as.dist(read.table("../data/snps/sintFiltSnps.ibsMat"), diag = TRUE)

# Test IBD
set.seed(694)
snpIBD = mantel.randtest(dGeo, snpDist)
snpIBD
## Monte-Carlo test
## Call: mantel.randtest(m1 = dGeo, m2 = snpDist)
## 
## Observation: -0.01026129 
## 
## Based on 999 replicates
## Simulated p-value: 0.721 
## Alternative hypothesis: greater 
## 
##       Std.Obs   Expectation      Variance 
## -0.6157953294  0.0001775375  0.0002873628

SNP Mantel plot

snpGenDist =  melt(as.matrix(snpDist), varnames = c("row", "col"), value.name = "dist")
snpGenDist = snpGenDist[snpGenDist$row != snpGenDist$col,]

geo = melt(as.matrix(dGeo), varnames = c("row", "col"), value.name = "geo")
geo = geo[geo$row != geo$col,]

snpMantelDF = data.frame(cbind(snpGenDist$dist, geo$geo))
colnames(snpMantelDF) = c("dist", "geo")

snpMantelA = ggplot(data = snpMantelDF, aes(x = geo, y = dist)) +
  scale_fill_gradientn(colors = paletteer_d("wesanderson::Zissou1")) +
  stat_density_2d(aes(fill = stat(density)), n = 300, contour = FALSE, geom = "raster") +
  geom_smooth(method = lm, col = "black", fill = "gray40", fullrange = TRUE) +
  geom_point(shape = 21, fill = "gray40", alpha = 0.25) +
  scale_x_continuous(limits = c(0,300), expand = c(0,0)) +
  scale_y_continuous(limits = c(0.25,0.5), breaks = seq(0.25,0.5, by = 0.05), expand = c(0,0)) +
  annotate("label", x = 225, y = 0.46, 
           label = paste("r = ", round(snpIBD$obs, 3), "; p = ", snpIBD$pvalue), 
           size = 4, alpha = 0.6) +             
  labs(x = "Geographic distance (km)", y = expression(paste("Genetic distance "))) +
  ggtitle("SNP") +
  theme_bw()

snpMantel = snpMantelA + theme(
  axis.title.x = element_blank(),
  axis.text.x = element_text(size = 12, color = "black"),
  axis.ticks.x = element_line(color = "black"),
  axis.line.x = element_blank(),
  axis.title.y = element_text(color = "black"),
  axis.text.y = element_text(size = 12, color = "black"),
  axis.ticks.y = element_line(color = "black"),
  axis.line.y = element_blank(),
  panel.border = element_rect(size = 1.2, color = "black"),
  plot.margin = margin(0.2,0.5,0.1,0.1, unit = "cm"),
  legend.position = "none")

snpMantel
## `geom_smooth()` using formula = 'y ~ x'


Distance-based redundancy analysis (dbRDA)

# Set directory for sdmPredictors to download/search for previously downloaded datasets 
# Also allow more than 60 seconds to download larger datasets before timing out
options(sdmpredictors_datadir="../data/snps/bioOracle", timeout = max(300, getOption("timeout")))

sintMa = as.dist(read.table("../data/snps/sintFiltSnps.ibsMat")[-131,-131])
popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 133, 164, 166, 209, 211),] %>% dplyr::select(sample, site, depthZone, latDD, longDD, depthM)

popData$sample = gsub("\\.[1-3]", "", popData$sample)

datasets = list_datasets(terrestrial = FALSE, marine = TRUE, freshwater = FALSE)

# Extract present-day data sets
present = list_layers(datasets) %>% dplyr::select(dataset_code, layer_code, name, units, description, contains("cellsize"), version) %>% filter(version == 22) %>% filter(layer_code %in% c("BO22_damean", "BO22_parmean", "BO22_ph", "BO22_curvelmax_bdmean", "BO22_salinitymean_bdmean", "BO22_salinitymean_ss", "BO22_curvelmean_ss", "BO22_curvelmean_bdmean", "BO22_dissoxmean_bdmean", "BO22_lightbotmax_bdmean", "BO22_lightbotmean_bdmean", "BO22_nitratemean_bdmean", "BO22_tempmax_bdmean", "BO22_tempmean_bdmean", "BO22_tempmean_ss", "BO22_ppmean_ss", "BO22_dissoxmean_ss", "BO22_ppmean_bdmean", "BO22_chlomean_ss", "BO22_chlomean_bdmean"))

envVar = load_layers(present$layer_code)

symType = read.csv("../data/ITS2/148_20210301_DBV_20210401T112728.profiles.absolute.abund_CLEAN.csv", header = TRUE, check.names = FALSE) 

symType = symType %>% mutate(sum = apply(symType[, c(2:(length(symType[1,])-1))], 1, function(x) {sum(x, na.rm = T)}))

symTypes = symType %>% dplyr::select(sample = Sample) %>% cbind(. ,(symType[, c(2:20)] / symType$sum))

head(symTypes)
##   sample A3-A3b-A3at-A3ax A3-A3at-A3b-A3q-A3s A3-A3s-A3q A3 A3-A3b-A3av-A3au-A3aw A4
## 1 SFK115                0          0.00000000          0  0                     0  0
## 2 SFK022                0          0.00000000          0  0                     0  0
## 3 SFK025                0          0.02520436          0  0                     0  0
## 4 SFK095                0          0.00000000          0  0                     0  0
## 5 SFK170                0          0.00000000          0  0                     0  0
## 6 SFK175                0          0.00000000          0  0                     0  0
##   B18b B18c B5  C3/C3.10 C1/C3-C42.2-C1dl-C3gl-C3gm-C3gk C3-C1-C3.10 C3-C1dk-C15hx
## 1    0    0  0 1.0000000                               0           0             0
## 2    0    0  0 1.0000000                               0           0             0
## 3    0    0  0 0.9747956                               0           0             0
## 4    0    0  0 1.0000000                               0           0             0
## 5    0    0  0 1.0000000                               0           0             0
## 6    0    0  0 1.0000000                               0           0             0
##   C3-C3go-C6c-C3gq-C3gp-C3gn-C3dw C16/C3-C16b C3-C3hb-C3ge-C3hc-C1dk
## 1                               0           0                      0
## 2                               0           0                      0
## 3                               0           0                      0
## 4                               0           0                      0
## 5                               0           0                      0
## 6                               0           0                      0
##   C3-C3gr-C3gt-C3gs-C3.10 C3/C1 C3
## 1                       0     0  0
## 2                       0     0  0
## 3                       0     0  0
## 4                       0     0  0
## 5                       0     0  0
## 6                       0     0  0
# check that all proportions add up to 1
apply(symTypes[, c(2:20)], 1, function(x) {
sum(x, na.rm = T)
})
##   [1] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [10] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [19] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [28] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [37] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [46] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [55] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [64] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [73] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [82] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
##  [91] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [100] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [109] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [118] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [127] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [136] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [145] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [154] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [163] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [172] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [181] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [190] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [199] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [208] 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## [217] 1.000000 2.981776 1.000000
symTypes = otuStack(symTypes, count.columns = c(2:length(symTypes[20, ])), condition.columns = c(1:1)) %>% filter(otu != "summ") %>% droplevels() %>% rename(sample = 3) 

symTypes = symTypes %>% group_by(sample) %>% summarise(count = max(count)) %>% left_join(symTypes %>% dplyr::select(sample, otu, count)) %>% rename(sym = otu) %>% dplyr::select(-count)
## Joining with `by = join_by(sample, count)`
symTypes$sym = as.numeric(symTypes$sym)

envData = data.frame(popData, raster::extract(envVar, popData[,5:4])) %>% left_join(symTypes) %>% cbind(pcangsd[-131,c(6:8)])
## Joining with `by = join_by(sample)`
# See what data are highly colinear
corData = rcorr(as.matrix(envData[,c(7:ncol(envData))]), type = "pearson")
corDataFlat = melt(corData$r, value.name = "r")
pDataFlat = melt(corData$P, value.name = "p")
corDataBind = corDataFlat %>% left_join(pDataFlat, by = c("Var1","Var2"))

ggplot(corDataBind) +
  geom_tile(aes(x = Var1, y = Var2, fill = r)) +
  scale_fill_gradient2(low = "#3B9AB2FF", high = "#F21A00FF") +
  geom_text(data = filter(corDataBind, r >= 0.7, p < 0.05),aes(x = Var1, y = Var2, label = round(r, 2))) +
    geom_text(data = filter(corDataBind, r <= -0.7, p < 0.05),aes(x = Var1, y = Var2, label = round(r, 2))) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90))

# Removing correlated data and biologically irrelevant measurements

envData2 = envData %>% dplyr::select("BO22_curvelmean_ss", "depthM", "BO22_lightbotmean_bdmean", "BO22_tempmean_bdmean", "BO22_ppmean_bdmean", "sym", "PC1", "PC2", "PC3")

# Checking again to make sure we've eliminated all colinearity
corData2 = cor(envData2)
corMelt2 = melt(corData2)

ggplot(corMelt2) +
  geom_tile(aes(x = Var1, y = Var2, fill = value)) +
  scale_fill_gradient2(low = "#3B9AB2FF", high = "#F21A00FF") +
  geom_text(data = corMelt2[corMelt2$value >= 0.7,],aes(x = Var1, y = Var2, label = round(value, 2))) +
    geom_text(data = corMelt2[corMelt2$value <= -0.7,],aes(x = Var1, y = Var2, label = round(value, 2))) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90))

Looks good now!

Now we check variance inflation factors(VIF) to see if there is any multi-colinearity (VIF ≥ 10) between explanatory variables

vif = diag(solve(cor(envData2)))
vif
##       BO22_curvelmean_ss                   depthM BO22_lightbotmean_bdmean 
##                 2.132407                 1.823732                 3.924312 
##     BO22_tempmean_bdmean       BO22_ppmean_bdmean                      sym 
##                 3.450330                 3.478778                 1.201146 
##                      PC1                      PC2                      PC3 
##                 1.085288                 1.176733                 1.018379

No issues with the VIF measures so we can continue to run the redundancy analysis

dbrda0 = dbrda(sintMa ~ 1, data = envData2)

dbrda1 = dbrda(sintMa ~ + depthM + BO22_curvelmean_ss + BO22_lightbotmean_bdmean + BO22_tempmean_bdmean + BO22_ppmean_bdmean + Condition(sym) + Condition(PC1 + PC2 + PC3), data = envData2)

# dbrda1 = dbrda(sintMa ~ BO22_curvelmean_ss + depthM + BO22_lightbotmean_bdmean + BO22_tempmean_bdmean + BO22_ppmean_bdmean + Condition(`A3-A3b-A3at-A3ax` + `A3-A3at-A3b-A3q-A3s` + `A3-A3s-A3q`  + A3 + `A3-A3b-A3av-A3au-A3aw` + A4 + B18b + B5 + `C3/C3.10` + `C1/C3-C42.2-C1dl-C3gl-C3gm-C3gk` + `C3-C1-C3.10` + `C3-C1dk-C15hx` + `C3-C3go-C6c-C3gq-C3gp-C3gn-C3dw` + `C16/C3-C16b` + `C3-C3hb-C3ge-C3hc-C1dk` + `C3-C3gr-C3gt-C3gs-C3.10` + `C3/C1` + C3 + PC1 + PC2 + PC3 + PC4), data = envData2)

anova(dbrda1)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = sintMa ~ +depthM + BO22_curvelmean_ss + BO22_lightbotmean_bdmean + BO22_tempmean_bdmean + BO22_ppmean_bdmean + Condition(sym) + Condition(PC1 + PC2 + PC3), data = envData2)
##           Df SumOfSqs      F Pr(>F)    
## Model      5   0.6329 1.7469  0.001 ***
## Residual 209  15.1447                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RsquareAdj(dbrda1)
## $r.squared
## [1] 0.0392843
## 
## $adj.r.squared
## [1] 0.01711071
summary(dbrda1)
## 
## Call:
## dbrda(formula = sintMa ~ +depthM + BO22_curvelmean_ss + BO22_lightbotmean_bdmean +      BO22_tempmean_bdmean + BO22_ppmean_bdmean + Condition(sym) +      Condition(PC1 + PC2 + PC3), data = envData2) 
## 
## Partitioning of squared Unknown distance:
##               Inertia Proportion
## Total         16.1117    1.00000
## Conditioned    0.3341    0.02074
## Constrained    0.6329    0.03928
## Unconstrained 15.1447    0.93998
## 
## Eigenvalues, and their contribution to the squared Unknown distance 
## after removing the contribution of conditiniong variables
## 
## Importance of components:
##                       dbRDA1   dbRDA2   dbRDA3   dbRDA4   dbRDA5    MDS1   MDS2    MDS3
## Eigenvalue            0.3472 0.089338 0.071386 0.065761 0.059286 1.13217 0.5365 0.43447
## Proportion Explained  0.0220 0.005662 0.004525 0.004168 0.003758 0.07176 0.0340 0.02754
## Cumulative Proportion     NA       NA       NA       NA       NA      NA     NA      NA
##                          MDS4    MDS5    MDS6     MDS7     MDS8     MDS9    MDS10
## Eigenvalue            0.23866 0.20365 0.16656 0.154994 0.145821 0.143957 0.136683
## Proportion Explained  0.01513 0.01291 0.01056 0.009824 0.009242 0.009124 0.008663
## Cumulative Proportion      NA      NA      NA       NA       NA       NA       NA
##                          MDS11    MDS12    MDS13    MDS14    MDS15    MDS16    MDS17
## Eigenvalue            0.136035 0.133278 0.130692 0.130369 0.129329 0.127733 0.124623
## Proportion Explained  0.008622 0.008447 0.008283 0.008263 0.008197 0.008096 0.007899
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS18    MDS19    MDS20    MDS21    MDS22    MDS23    MDS24
## Eigenvalue            0.123902 0.122050 0.120676 0.119665 0.119552 0.117597 0.116450
## Proportion Explained  0.007853 0.007736 0.007649 0.007584 0.007577 0.007453 0.007381
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS25    MDS26    MDS27    MDS28    MDS29    MDS30    MDS31
## Eigenvalue            0.115028 0.113536 0.112712 0.112382 0.111062 0.110364 0.109783
## Proportion Explained  0.007291 0.007196 0.007144 0.007123 0.007039 0.006995 0.006958
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                        MDS32    MDS33    MDS34    MDS35    MDS36    MDS37    MDS38
## Eigenvalue            0.1089 0.108623 0.107028 0.106477 0.105963 0.103989 0.103393
## Proportion Explained  0.0069 0.006885 0.006784 0.006749 0.006716 0.006591 0.006553
## Cumulative Proportion     NA       NA       NA       NA       NA       NA       NA
##                          MDS39    MDS40    MDS41    MDS42    MDS43    MDS44    MDS45
## Eigenvalue            0.102453 0.101956 0.100612 0.100276 0.099940 0.098532 0.097567
## Proportion Explained  0.006494 0.006462 0.006377 0.006356 0.006334 0.006245 0.006184
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS46   MDS47    MDS48    MDS49    MDS50   MDS51    MDS52
## Eigenvalue            0.097265 0.09625 0.095340 0.094438 0.094246 0.09404 0.093313
## Proportion Explained  0.006165 0.00610 0.006043 0.005986 0.005973 0.00596 0.005914
## Cumulative Proportion       NA      NA       NA       NA       NA      NA       NA
##                          MDS53    MDS54    MDS55    MDS56    MDS57    MDS58    MDS59
## Eigenvalue            0.092239 0.091217 0.090532 0.090111 0.089627 0.088528 0.087811
## Proportion Explained  0.005846 0.005781 0.005738 0.005711 0.005681 0.005611 0.005566
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS60    MDS61    MDS62   MDS63    MDS64    MDS65    MDS66
## Eigenvalue            0.087497 0.087016 0.086019 0.08583 0.085099 0.084665 0.083446
## Proportion Explained  0.005546 0.005515 0.005452 0.00544 0.005394 0.005366 0.005289
## Cumulative Proportion       NA       NA       NA      NA       NA       NA       NA
##                          MDS67    MDS68    MDS69    MDS70    MDS71    MDS72    MDS73
## Eigenvalue            0.082733 0.082587 0.081325 0.080522 0.080041 0.079601 0.079123
## Proportion Explained  0.005244 0.005234 0.005154 0.005104 0.005073 0.005045 0.005015
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS74    MDS75    MDS76    MDS77    MDS78    MDS79    MDS80
## Eigenvalue            0.078437 0.078193 0.077911 0.076857 0.075719 0.075360 0.074930
## Proportion Explained  0.004971 0.004956 0.004938 0.004871 0.004799 0.004776 0.004749
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS81    MDS82   MDS83    MDS84    MDS85    MDS86    MDS87
## Eigenvalue            0.073663 0.073583 0.07320 0.072117 0.071391 0.070893 0.070479
## Proportion Explained  0.004669 0.004664 0.00464 0.004571 0.004525 0.004493 0.004467
## Cumulative Proportion       NA       NA      NA       NA       NA       NA       NA
##                          MDS88    MDS89    MDS90    MDS91    MDS92    MDS93    MDS94
## Eigenvalue            0.070036 0.069639 0.069008 0.068262 0.067923 0.067656 0.067189
## Proportion Explained  0.004439 0.004414 0.004374 0.004327 0.004305 0.004288 0.004258
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                          MDS95    MDS96    MDS97   MDS98   MDS99   MDS100   MDS101
## Eigenvalue            0.066717 0.066160 0.065838 0.06532 0.06468 0.064226 0.062833
## Proportion Explained  0.004229 0.004193 0.004173 0.00414 0.00410 0.004071 0.003982
## Cumulative Proportion       NA       NA       NA      NA      NA       NA       NA
##                         MDS102   MDS103   MDS104  MDS105   MDS106   MDS107   MDS108
## Eigenvalue            0.062377 0.061830 0.061480 0.06106 0.060563 0.059595 0.059211
## Proportion Explained  0.003954 0.003919 0.003897 0.00387 0.003839 0.003777 0.003753
## Cumulative Proportion       NA       NA       NA      NA       NA       NA       NA
##                         MDS109   MDS110   MDS111   MDS112   MDS113   MDS114   MDS115
## Eigenvalue            0.059040 0.058569 0.057573 0.057004 0.056860 0.056296 0.055666
## Proportion Explained  0.003742 0.003712 0.003649 0.003613 0.003604 0.003568 0.003528
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                        MDS116   MDS117   MDS118   MDS119   MDS120   MDS121   MDS122
## Eigenvalue            0.05522 0.054880 0.054085 0.053303 0.052808 0.052191 0.051891
## Proportion Explained  0.00350 0.003478 0.003428 0.003378 0.003347 0.003308 0.003289
## Cumulative Proportion      NA       NA       NA       NA       NA       NA       NA
##                         MDS123  MDS124   MDS125   MDS126   MDS127   MDS128   MDS129
## Eigenvalue            0.051415 0.05097 0.050679 0.050049 0.049275 0.048875 0.047835
## Proportion Explained  0.003259 0.00323 0.003212 0.003172 0.003123 0.003098 0.003032
## Cumulative Proportion       NA      NA       NA       NA       NA       NA       NA
##                         MDS130   MDS131   MDS132   MDS133   MDS134   MDS135   MDS136
## Eigenvalue            0.047567 0.047128 0.046640 0.046206 0.045154 0.044480 0.044005
## Proportion Explained  0.003015 0.002987 0.002956 0.002929 0.002862 0.002819 0.002789
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                         MDS137   MDS138   MDS139   MDS140   MDS141   MDS142   MDS143
## Eigenvalue            0.043769 0.043212 0.042364 0.041953 0.041248 0.040668 0.040566
## Proportion Explained  0.002774 0.002739 0.002685 0.002659 0.002614 0.002578 0.002571
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                         MDS144   MDS145   MDS146   MDS147   MDS148   MDS149   MDS150
## Eigenvalue            0.040271 0.039644 0.039257 0.038613 0.037987 0.037653 0.037377
## Proportion Explained  0.002552 0.002513 0.002488 0.002447 0.002408 0.002386 0.002369
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                         MDS151   MDS152   MDS153   MDS154   MDS155   MDS156   MDS157
## Eigenvalue            0.036558 0.036232 0.035646 0.035116 0.034859 0.034180 0.033893
## Proportion Explained  0.002317 0.002296 0.002259 0.002226 0.002209 0.002166 0.002148
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                         MDS158   MDS159   MDS160  MDS161   MDS162   MDS163   MDS164
## Eigenvalue            0.033327 0.032593 0.032381 0.03171 0.031278 0.030993 0.029650
## Proportion Explained  0.002112 0.002066 0.002052 0.00201 0.001982 0.001964 0.001879
## Cumulative Proportion       NA       NA       NA      NA       NA       NA       NA
##                         MDS165   MDS166   MDS167   MDS168   MDS169   MDS170   MDS171
## Eigenvalue            0.028960 0.028706 0.028262 0.027876 0.027420 0.026812 0.026649
## Proportion Explained  0.001835 0.001819 0.001791 0.001767 0.001738 0.001699 0.001689
## Cumulative Proportion       NA       NA       NA       NA       NA       NA       NA
##                         MDS172   MDS173   MDS174   MDS175   MDS176  MDS177   MDS178
## Eigenvalue            0.025993 0.025172 0.024269 0.024099 0.023912 0.02224 0.021955
## Proportion Explained  0.001647 0.001595 0.001538 0.001527 0.001516 0.00141 0.001392
## Cumulative Proportion       NA       NA       NA       NA       NA      NA       NA
##                         MDS179   MDS180   MDS181   MDS182  MDS183   MDS184   MDS185
## Eigenvalue            0.021407 0.020954 0.020397 0.020069 0.01972 0.019044 0.017646
## Proportion Explained  0.001357 0.001328 0.001293 0.001272 0.00125 0.001207 0.001118
## Cumulative Proportion       NA       NA       NA       NA      NA       NA       NA
##                         MDS186   MDS187   MDS188    MDS189  MDS190    MDS191   MDS192
## Eigenvalue            0.016709 0.016501 0.016131 0.0157242 0.01483 0.0137934 0.013238
## Proportion Explained  0.001059 0.001046 0.001022 0.0009966 0.00094 0.0008742 0.000839
## Cumulative Proportion       NA       NA       NA        NA      NA        NA       NA
##                          MDS193    MDS194    MDS195   MDS196    MDS197   MDS198
## Eigenvalue            0.0129572 0.0114694 0.0111595 0.010303 0.0100368 0.009971
## Proportion Explained  0.0008212 0.0007269 0.0007073 0.000653 0.0006361 0.000632
## Cumulative Proportion        NA        NA        NA       NA        NA       NA
##                          MDS199    MDS200    MDS201    MDS202    MDS203      iMDS1
## Eigenvalue            0.0080929 0.0071610 0.0058339 0.0046271 0.0025655 -0.0007415
## Proportion Explained  0.0005129 0.0004539 0.0003698 0.0002933 0.0001626  0.0000470
## Cumulative Proportion        NA        NA        NA        NA        NA         NA
##                            iMDS2      iMDS3     iMDS4     iMDS5     iMDS6
## Eigenvalue            -0.0039731 -0.0078746 -0.026305 -0.056153 -0.082369
## Proportion Explained   0.0002518  0.0004991  0.001667  0.003559  0.005221
## Cumulative Proportion         NA         NA        NA        NA        NA
## 
## Accumulated constrained eigenvalues
## Importance of components:
##                       dbRDA1  dbRDA2  dbRDA3  dbRDA4  dbRDA5
## Eigenvalue            0.3472 0.08934 0.07139 0.06576 0.05929
## Proportion Explained  0.5485 0.14115 0.11279 0.10390 0.09367
## Cumulative Proportion 0.5485 0.68965 0.80243 0.90633 1.00000
## 
## Scaling 2 for species and site scores
## * Species are scaled proportional to eigenvalues
## * Sites are unscaled: weighted dispersion equal on all dimensions
## * General scaling constant of scores:  7.698385 
## 
## 
## Site scores (weighted sums of species scores)
## 
##        dbRDA1     dbRDA2    dbRDA3    dbRDA4     dbRDA5       MDS1
## 1   -0.958092  0.0282963  0.135254 -1.094064 -0.7040718  0.0539183
## 2   -0.189929 -1.4013993  0.254329 -0.317715  0.2249735 -0.0142703
## 3   -0.409004 -1.6017640  0.141864 -0.341630 -0.8989788 -0.0184982
## 4   -0.665664 -0.0180418 -0.605567 -0.760048 -0.1980064  0.0352832
## 5   -0.593225 -0.4057810 -0.307845  0.058549 -0.3191410 -0.0970398
## 6   -0.714939 -0.0293426 -0.319117 -0.458123 -0.6080646 -0.0434113
## 7   -0.097472 -1.4911936  0.245144 -0.148002  0.2159028  0.0076448
## 8   -0.077240 -1.5316728 -0.074734 -0.077088 -0.6685379 -0.0365348
## 9   -0.756609  0.1727393  0.077714 -0.926899 -0.0998855  0.0207745
## 10   0.184246 -0.7210670 -0.020671 -0.618763 -1.4221415  0.7881829
## 11  -0.519007  0.1147323  0.178037 -0.103376 -1.0699746 -0.0630512
## 12  -0.524515 -0.4674684  0.281386 -0.084086  0.0499855  0.1950999
## 13  -0.568336 -0.5384645  0.334818 -0.474620 -0.2699750  0.1600174
## 14  -0.529622 -0.1228112  0.174642 -0.395585 -0.4385787  0.1424753
## 15  -0.374114 -0.3843007 -0.266898 -1.081812 -0.1826775  0.0052114
## 16  -0.601878  0.0289132  0.250920 -0.313122  0.2256850  0.1025325
## 17  -0.669495 -0.4149300 -0.430739  0.067214  0.0449057  0.0947359
## 18  -0.292468 -0.1394168 -0.623618 -0.472874  0.1220129  0.0069961
## 19  -0.549377 -0.1679656  0.122769 -0.337018  0.1626708  0.0905625
## 20  -0.560393 -0.0004312 -0.339235 -0.277357  0.0848161  0.0662717
## 21  -0.404441  0.1490385 -0.281287 -0.161973  0.2492786 -0.0334475
## 22  -0.146232 -1.4510778 -0.055787 -0.536190  0.2963315  0.0205269
## 23  -0.634971 -0.5829102  0.119804 -0.298849  0.1504781  0.1253284
## 24  -0.565064 -0.1758305  0.028273 -0.989359 -0.5750723  0.0574187
## 25  -0.527696  0.2504819 -0.020750 -0.482907 -0.7271439  0.0662403
## 26  -0.477417  0.5774791 -0.320389 -0.981302 -0.2396549  0.5554301
## 27   1.209326 -0.8706270  0.254494 -1.213133  0.9432368 -0.1665357
## 28   1.362094 -1.1483596  0.036282 -1.025100 -0.3708283 -0.2471315
## 29  -0.530767  0.0173027  0.093015 -0.507598 -0.3061602  0.0746474
## 30  -0.423215 -0.5862369  0.231965 -0.045656 -0.3671395  0.0947337
## 31  -0.708786 -0.4876734 -0.085876 -0.409099 -0.0881512  0.2569152
## 32  -0.296126 -0.9521295  0.567446 -1.021254 -0.3149379  0.0871209
## 33  -0.519156 -0.4162350  0.371873 -1.299579 -1.4577116 -0.0503865
## 34  -0.530932 -0.6433283 -0.043663 -0.787066 -0.5833680  0.1147751
## 35   2.093101  1.2252598 -0.475655 -0.370135 -0.2088041 -0.9878631
## 36   2.137381  0.8405933 -0.188478  0.101753 -0.6373037 -1.1035998
## 37   2.004044  1.1473468 -0.168985 -0.546596 -1.1627570 -1.0740192
## 38   1.352943 -0.4233771 -0.440797 -2.017505  0.3716449 -0.2731243
## 39   1.279767 -1.6220176  0.543853 -1.154689 -0.2833538 -0.4185867
## 40  -0.402379 -0.3284630 -0.422010  0.311513 -0.3491938  0.3667999
## 41   0.001842 -1.3395638  0.693724  0.068341  0.7389011  0.4139066
## 42   1.192460 -1.2205653  0.151105 -0.475765  0.9590677 -0.3380569
## 43  -0.248581 -0.6223734 -0.093118 -0.270057  0.3540632  0.3984780
## 44   0.051513 -1.0224530  0.050861 -0.216902  0.4287194  0.4359347
## 45   1.368464 -0.7927421  0.085754 -1.303172  0.3556839 -0.5579218
## 46  -0.416946 -0.0615434 -0.142671 -0.494692 -0.4071488  0.4463323
## 47   1.338181 -1.2953385  0.439762 -0.777123  0.9329817 -0.3285473
## 48   0.082088 -1.2052326  0.063761 -0.282929  0.0486606  0.3972309
## 49   0.005711 -1.2105855  0.159553 -0.093373  0.2002883  0.5085030
## 50   0.628734 -1.1234784 -0.282374 -1.535752  0.3611495 -0.1296543
## 51   0.662322 -1.0895188 -0.040685 -0.327417 -0.5137929 -0.3254478
## 52   0.027586 -1.6834781  0.392997  0.222569  0.0106723  0.3125284
## 53  -0.042286 -1.0353069 -0.076041 -0.453493  0.0637228  0.4894294
## 54   0.132765 -1.0436498 -0.030497 -0.337366 -0.0627682  0.4520800
## 55   1.243236 -0.7637312  0.175608 -0.896416  0.3913693 -0.3978772
## 56  -0.551637  0.0618163 -0.375035  0.005840  0.4016451  0.0249840
## 57  -0.605099 -0.6788424 -0.078736  0.769568  0.0223534  0.0677022
## 58  -0.621454 -0.6319862 -0.195309  0.090707 -0.1963501  0.1112188
## 59  -0.655171 -0.4221642 -0.283994  0.358046 -0.0586426  0.1535037
## 60  -0.034234 -1.6652359  0.015867  0.537762 -0.1589041  0.1235329
## 61  -0.576534 -0.6751064 -0.238167  0.668851 -0.8903912 -0.0478118
## 62   2.017459  1.3973568 -0.567109  0.599263 -0.4734965 -1.1152300
## 63  -0.324763  0.3540563 -0.208637  0.406440 -0.9176961  0.3991233
## 64   2.088873  0.8211856 -0.797011 -0.002722  0.0469620 -1.1771015
## 65  -0.250605  0.4616084 -0.500598 -0.153151 -0.3897077  0.3871496
## 66   1.996148  0.8529758 -0.244349  0.416837 -0.0919431 -1.0985713
## 67  -0.583260  0.3707199 -0.759407  0.935600  0.2763962  0.5457172
## 68  -0.206924  0.1497721 -0.718411 -0.125876 -0.9821063  0.4178067
## 69  -0.111227 -0.7745797 -0.057931 -0.170006 -0.6438915  0.4882285
## 70  -0.373405  0.6678923 -1.106632  0.217170 -0.5105318  0.3377364
## 71   1.963797  0.6962202 -0.346981  0.299776 -0.3574861 -1.0525845
## 72   2.177980  0.3382953 -0.535465  0.763365  0.4868119 -1.3180021
## 73  -0.666956 -0.2556319 -0.768200  0.731735  1.0520752  0.3847154
## 74  -0.045661 -1.2927497  0.050534  0.906687 -0.0649490  0.3478351
## 75   1.222686 -2.2803470 -0.141374 -0.285968  0.2307576 -0.5964307
## 76  -0.245254 -1.4832115  0.387779  0.611112  0.1404870  0.5573625
## 77  -0.450142  0.2646452 -0.681773  0.356602  0.0246936  0.2963903
## 78   1.989246  0.2839925 -0.196521  0.667056  0.4209691 -1.2083557
## 79   1.281929 -1.7094552 -0.590324 -0.800235  0.5386098 -0.5066440
## 80   1.300340  0.5058559 -0.482290  1.303957 -0.1855409 -1.0770898
## 81  -0.578911 -0.7600305 -0.426256  0.252298  0.8293844  0.4535574
## 82  -0.057613 -1.5915910 -0.104044  0.776867  0.2282950  0.3478446
## 83   0.048270 -1.2203962 -0.113433  0.639420  0.1996132  0.2269844
## 84  -0.284947 -0.3178527 -0.582819  0.532445 -0.5260942  0.2462332
## 85  -0.658168 -1.8457763 -1.074475  1.341302 -0.3736645  0.3121944
## 86  -0.390418 -0.6632574  0.231323  1.039035  0.6238943  0.4333352
## 87  -0.480141 -0.4772757 -0.230029  0.314628  0.0142333  0.3579172
## 88  -0.366897 -0.4541809  0.041934  0.795778 -0.0900206  0.2863619
## 89  -0.428873 -0.0672562 -0.426162  0.389876  0.5431640  0.2824523
## 90  -0.505536 -0.0806299 -0.681398  0.248500  0.6500091  0.3435024
## 91  -0.519041 -0.3103439 -0.215931  0.702658  0.7528600  0.3395287
## 92  -0.613756 -0.5408061 -0.291840 -0.102884  0.4419416  0.0093066
## 93  -0.087765 -1.9135043  0.020296  0.775773  0.1403660  0.0489276
## 94  -0.232633 -1.5286149  0.102974  0.827350 -0.2688504  0.1187442
## 95   2.310608  0.6275199 -0.362713  0.700998 -0.0425590 -1.5333630
## 96  -0.165591 -1.3801314  0.112921  0.705202 -0.0822476  0.0810878
## 97  -0.631944 -1.1680879 -0.347091  1.654916 -0.7273523  0.2118162
## 98  -0.599755 -0.3356091 -0.934013  0.133741 -0.5055278  0.0294334
## 99   2.062424  0.5796733 -0.749233  1.475640 -0.0928271 -1.5378200
## 100 -0.672106 -0.8110040 -1.029495  0.528915  0.0008569  0.1964618
## 101 -0.728405  0.6117586  0.126589 -0.412450  0.4962948 -0.0482401
## 102 -0.622490  0.6691061 -0.559305 -1.379163  0.9921039 -0.0713981
## 103 -0.494061  0.6700189 -0.256563 -0.745768  0.7701545 -0.0524947
## 104 -0.831005  0.3563160 -0.447588 -0.373706  0.6923856  0.1581004
## 105 -0.482654  0.6074312 -0.468356 -0.242168  0.6895894 -0.0732986
## 106 -0.661026  0.8508348 -0.093309 -0.732291  0.0776106  0.0048946
## 107 -0.613142  0.2991404  0.244322 -0.503050  0.4634309  0.0523044
## 108 -0.524356  0.3513916 -0.279959 -0.677823  0.4115385 -0.0794864
## 109 -0.483426  0.4939469 -0.158015 -0.304297  0.8699131 -0.0222836
## 110 -0.651194  0.3571680  0.213708 -0.129656  0.2122619  0.0562494
## 111 -0.598606  0.6601949 -0.441575 -0.489530  0.5289278  0.0326903
## 112 -0.518466  0.2001003 -0.373822 -0.298191  0.2303503 -0.0374668
## 113 -0.483058  0.4292687 -0.058230 -0.208731  0.3439509 -0.0561353
## 114 -0.686464  0.5943296 -0.413346 -0.804803  0.6969381  0.0593755
## 115 -0.782638 -0.0202665 -0.253278 -0.551012  0.8012648  0.0487897
## 116 -0.506673  0.4379316 -0.330898 -0.491857  0.4882212  0.0103063
## 117 -0.740987 -0.2079897  0.165297 -0.705695  1.0704987  0.1623066
## 118 -0.596835  0.5702444 -0.473672 -0.724764  0.9654928  0.0882793
## 119 -0.498793  0.2692308 -0.305809 -0.589233  0.5339630  0.0310829
## 120 -0.165169  0.7516254  0.193959 -0.698138  0.4397509  0.0329277
## 121 -0.727533  0.5934914  0.023389 -0.496501  0.5262761  0.1208888
## 122 -0.610815  0.3025016 -0.425205 -0.430005  1.7535256  0.1509402
## 123 -0.692408  0.6216373 -0.034842 -0.736440  0.7957061  0.1308796
## 124 -0.633859  0.2902985 -0.145818 -0.288265  1.4210161  0.0465747
## 125 -0.594930  0.3957057  0.221401 -0.847367  0.2308287  0.0218218
## 126 -0.971204  0.4894040 -0.026163 -0.178798 -0.1527787  0.0913330
## 127 -0.822900  0.5247036 -0.326726 -0.668734  0.1187894  0.0701855
## 128 -0.561266  0.4453228 -0.033569 -0.178786  1.0543294  0.4488227
## 129 -0.439954  0.6211536 -0.176561 -0.111666  0.1999472  0.3963692
## 130 -0.138979 -0.7793800  0.016116  0.816266  0.4689073  0.5226288
## 132  2.056372  1.1422465 -0.135156  0.765991  0.3698284 -1.1240865
## 133  0.654873 -0.5135329  0.179191 -1.114600  1.1142707 -0.0636149
## 134  0.215998 -1.4563338  0.338931  0.389654  1.4787820  0.5265838
## 135 -0.452837  0.1827560 -0.049083  0.169756  0.5801969  0.4443321
## 136 -0.381488  0.1601039  0.298174  0.550960  0.2188644  0.3900800
## 137 -0.796048 -0.2581890  0.780022  0.618169  0.8001680  0.8752436
## 138 -0.465395  0.2713363  0.265163 -0.547561  0.6312787  0.4800753
## 139 -0.373715  0.7078792 -0.056828  0.522446  1.3763081  0.4690289
## 140 -0.331106  0.7787593 -0.121280 -0.249932  0.6120459  0.3368191
## 141  2.066178  1.6455727 -0.192520 -0.059376  0.5380941 -1.2528258
## 142  2.202566  1.4048547 -0.469314  0.285962  0.2803642 -1.2677078
## 143  1.564889  0.8147619 -0.622053  0.993454 -0.4592405 -0.9058399
## 144  1.583441  1.3243571 -0.180755  0.857001 -0.2843578 -0.9612884
## 145  2.121366  1.5973627  0.195130  0.412698  0.7770012 -1.2006660
## 146 -0.158625 -0.8424404  0.125395  0.062472  0.4554021  0.5480511
## 147 -1.040501  0.7675997  0.100034  0.173925  0.8519423  0.7273726
## 148  2.100042  1.3068906  0.094051 -0.121465  0.5766700 -1.0901295
## 149  2.141745  0.7748737 -0.028607  0.338963 -0.1363600 -1.1648683
## 150  0.763622 -0.2398985  0.849938 -1.326033  1.1510280 -0.1112568
## 151  1.126839 -0.2985289  0.546794 -1.268548  1.5192720 -0.1572580
## 152 -0.980472  0.6545323 -0.494015  0.298117  0.2752153  0.5252666
## 153  2.084295  0.8916131 -0.253642  0.060665  0.1247914 -1.1184988
## 154 -0.890873  0.2683494 -0.392815 -0.625126  1.2240866  0.1261773
## 155 -1.182750  1.3653633 -0.499002 -0.058233 -0.3157744  0.1684962
## 156 -0.988048 -0.5990873  0.109260 -0.616131 -0.3166994  0.3336867
## 157 -0.450556 -0.2246822  0.325015  0.376952  0.7418578  0.4866559
## 158 -0.144453 -0.8397573  0.571193 -0.344653  1.4667346  0.7199798
## 159 -0.465061  0.0507013 -0.054130  0.194644  0.9700448  0.4237042
## 160 -0.766089 -0.4212740  0.550507  0.389213 -0.0689478  0.8398935
## 161 -0.331085 -0.1453427 -0.699814 -0.008546 -0.5756906  0.5182059
## 162 -0.291685  0.1644390 -0.460444 -0.413143 -1.5696093  0.4912875
## 163  2.170405  0.8314463 -0.001814 -0.441407 -0.5084539 -0.9351448
## 164  0.135604 -1.4796039  0.364684 -0.078711 -1.3967910  0.5870841
## 165  2.279455  1.4577452 -0.159296 -0.414322 -0.3389876 -0.9339759
## 166  2.245815  1.2750091 -0.052354 -0.553084 -1.0248791 -0.8828733
## 167 -0.395679  0.1022662 -0.072744 -0.108073 -1.3254898  0.5918133
## 168 -0.343048  0.5689776  0.489607 -0.995353 -1.3983387  0.6178046
## 169 -0.650274  1.3205382 -2.540393  1.702016 -0.2856397  0.2231687
## 170 -0.370119  1.1781166 -2.661244  1.028048 -0.3769037  0.1078557
## 171 -0.471095  0.9181225 -2.414583  1.557286 -0.3365751  0.2370105
## 172 -0.562942  1.0927425 -2.629605  1.093270 -0.1008366  0.2901946
## 173 -0.520079  0.0811879 -0.231053 -0.542944 -0.6411243  0.7817843
## 174 -0.091664 -0.6972292  0.128768 -1.593068 -0.3852385  0.9048440
## 175 -0.468496  0.3619587 -0.108275 -1.019317 -0.8678606  0.7669229
## 176  2.141788  1.3951965 -0.129766 -0.588584 -1.2668901 -0.8757004
## 177 -0.505369  0.7505751 -0.243031 -0.849841 -0.9015673  0.7308182
## 178  2.201976  1.2782898  0.259680  1.083171 -2.0488059 -0.8639895
## 179  2.185535  1.1157527 -0.733278 -0.729167 -0.8667293 -0.7315716
## 180 -0.696632  0.3569771  0.692983  0.674160  0.0431219  0.0540893
## 181 -0.617922  0.5827354  0.692056  0.406098 -0.1610349 -0.0476748
## 182 -0.571513  0.4512917  0.451266  0.628692 -0.1391120 -0.0956663
## 183  2.171292  1.0674869  0.837738  1.522925 -0.0928767 -1.0254803
## 184 -0.039154 -0.9854798  1.472425  0.929516  0.4032605  0.7126631
## 185  0.014010 -1.5047979  0.675158  1.166958  0.0537033  0.6261817
## 186  1.930092  1.2698966  0.780327  0.913292 -0.2406041 -0.9179649
## 187 -0.586363  0.5694148  0.766271  0.777369  0.1068661  0.5880553
## 188 -0.504434  0.9854630  1.588929  0.003891  0.2530023  0.6570990
## 189  2.017411  0.9609907 -0.035749  0.794698  0.3548855 -0.9868713
## 190 -0.507218  0.6460969  0.877312  0.080105  0.1413924  0.4960420
## 191 -0.040816 -1.3969076  1.203114  1.147266 -0.4529091  0.5142399
## 192  0.201697  0.6719876  1.093030  1.058243  0.4020731  0.2745040
## 193 -0.473234  0.8829355  0.846977  0.691294  0.8188920  0.5414325
## 194 -0.829135  0.1406403  1.097989  0.706329 -0.6612152  0.1604306
## 195 -0.625783  1.2351845  0.708505 -0.082058 -0.5629695 -0.1547960
## 196 -0.514659  0.7006030  0.062995  0.504514 -0.4895330 -0.2418915
## 197 -0.550574  0.1900873  0.226910  0.499347 -0.6979439 -0.0513287
## 198 -0.938203  0.2779070  0.998923  0.515973  0.0033094 -0.0077506
## 199 -0.647968  0.1668820  0.713711  0.382500 -0.2259156  0.0002466
## 200 -0.397673  1.3855278  0.108594  0.318759 -0.6628792 -0.1992138
## 201 -0.230006 -0.8633553  1.107223  0.624352 -0.9992816 -0.0234701
## 202 -0.572105  0.4685940  0.432170 -0.107308 -0.1301141 -0.0363704
## 203 -0.490485  0.2974118  0.517801  0.599826 -1.0825100 -0.2248306
## 204 -0.690984  0.0577895  0.442175  0.308468 -0.6128527 -0.0253304
## 205 -0.609871  0.2117476  0.468253  0.382039 -0.2387947 -0.0728607
## 206 -0.512814  0.8385612  0.716088  0.053365 -0.3970998 -0.0701689
## 207 -0.544435  0.4835073  0.364004 -0.164850 -0.8560329 -0.1634384
## 208 -0.649170  0.5144617  1.109394 -0.047505  0.4987412 -0.1419400
## 209 -0.241990 -0.3569793  0.177618  0.182606  0.3913970 -0.0497915
## 210 -0.695941  0.2989461  0.647431  0.284790 -0.8048091 -0.1040708
## 211 -0.663510  0.2190971  0.926076  0.132262 -0.1378519 -0.0043869
## 212 -0.583117  1.2174736  0.935023  0.277021 -1.3703462 -0.0813187
## 213 -0.260372 -0.5631690  1.704310  0.782938 -0.5499994  0.0477053
## 214 -0.731616  0.1004971  0.764989  0.462869 -0.3763565  0.0812733
## 215 -0.574167  1.1368202  0.043447  0.266202 -0.5631148 -0.0399757
## 216 -0.374368 -0.6260897  1.103084  0.854488 -1.0711013 -0.0073943
## 217 -0.603033  0.4446625  0.927840  0.462781  0.0443342  0.5780239
## 218  2.056833  1.6272676  0.834680  0.513827 -0.6884711 -1.0709812
## 219 -0.590164  0.2795861  0.686667  0.431109  0.2166065  0.5647443
## 220 -0.256623  0.0990163  1.205480  0.909677  0.1190216  0.5835910
## 
## 
## Site constraints (linear combinations of constraining variables)
## 
##       dbRDA1  dbRDA2      dbRDA3     dbRDA4     dbRDA5       MDS1
## 1   -0.63524 -0.4934 -0.06028263 -0.5491387 -0.2443752  0.0539183
## 2   -0.56799 -0.4819 -0.04607788 -0.5535456 -0.1760692 -0.0142703
## 3   -0.62013 -0.4801 -0.05752094 -0.5713032 -0.2079568 -0.0184982
## 4   -0.51668 -0.4960 -0.03848242 -0.5148231 -0.1816979  0.0352832
## 5   -0.59693 -0.5030 -0.06028334 -0.5236111 -0.2574377 -0.0970398
## 6   -0.69172 -0.5768 -0.08775366 -0.4046790 -0.5033842 -0.0434113
## 7   -0.59179 -0.4819 -0.05027595 -0.5590322 -0.1932317  0.0076448
## 8   -0.58567 -0.5039 -0.05686573 -0.5106055 -0.2763645 -0.0365348
## 9   -0.60308 -0.5039 -0.05944262 -0.5180343 -0.2729208  0.0207745
## 10   1.04306  0.2665 -0.41268700 -1.2617530 -0.8870629  0.7881829
## 11  -0.49070 -0.6171 -0.06135129 -0.2690133 -0.5509137 -0.0630512
## 12  -0.25681 -0.5278  0.01831556 -0.3888152 -0.1094606  0.1950999
## 13  -0.20841 -0.5256  0.02384645 -0.3828811 -0.0761863  0.1600174
## 14  -0.29422 -0.5206  0.00609335 -0.4130868 -0.1127747  0.1424753
## 15  -0.24826 -0.4743  0.02402919 -0.4964238  0.0553234  0.0052114
## 16  -0.25766 -0.5083  0.01305038 -0.4280893 -0.0671064  0.1025325
## 17  -0.31628 -0.5173 -0.00217491 -0.4261623 -0.1292519  0.0947359
## 18  -0.30562 -0.4990  0.01142283 -0.4597657 -0.0471206  0.0069961
## 19  -0.28493 -0.4961  0.01105411 -0.4592492 -0.0440237  0.0905625
## 20  -0.32863 -0.4969 -0.02021749 -0.4463262 -0.1663639  0.0662717
## 21  -0.31837 -0.5231  0.00367964 -0.4154721 -0.1310803 -0.0334475
## 22  -0.35835 -0.5196 -0.00635882 -0.4344151 -0.1429087  0.0205269
## 23  -0.36234 -0.5217 -0.00561700 -0.4304038 -0.1554951  0.1253284
## 24  -0.34268 -0.5217 -0.00064761 -0.4208845 -0.1471264  0.0574187
## 25  -0.31215 -0.5169 -0.00149104 -0.4280047 -0.1158781  0.0662403
## 26   0.55105  0.3671 -0.13707840 -0.5410884 -0.1097624  0.5554301
## 27   0.62834  0.3781 -0.10743535 -0.5492366  0.0053908 -0.1665357
## 28   0.62866  0.3852 -0.11539398 -0.5619791 -0.0002024 -0.2471315
## 29  -0.29627 -0.5206  0.00599765 -0.4118180 -0.1182391  0.0746474
## 30  -0.32483 -0.4988 -0.05351206 -0.4838162 -0.3026673  0.0947337
## 31  -0.21393 -0.5027 -0.03335401 -0.4518027 -0.2481103  0.2569152
## 32  -0.31110 -0.2494 -0.20532246 -0.7702347 -0.1339480  0.0871209
## 33  -0.55622 -0.5132 -0.03861105 -0.3312192 -0.8709895 -0.0503865
## 34  -0.29155 -0.5046 -0.04820973 -0.4672793 -0.3253319  0.1147751
## 35   0.64297  0.4081 -0.10614223 -0.6049870  0.0893366 -0.9878631
## 36   0.43217  0.2513 -0.18906432 -0.3486342 -0.5309144 -1.1035998
## 37   0.36289  0.2755 -0.25688877 -0.4363784 -0.7267333 -1.0740192
## 38   0.62910  0.3850 -0.11547467 -0.5623953  0.0003614 -0.2731243
## 39   0.26116 -0.6636  0.10148319  0.0009564 -0.1967889 -0.4185867
## 40   0.28855 -0.6576  0.10238700 -0.0014177 -0.1665915  0.3667999
## 41   0.42888 -0.5493  0.16160878 -0.1816810  0.2582620  0.4139066
## 42   0.46301 -0.5244  0.16991656 -0.2243850  0.3452163 -0.3380569
## 43   0.40331 -0.5237  0.09860360 -0.2612886  0.0801781  0.3984780
## 44   0.52884 -0.2527 -0.02670023 -0.5540140  0.3559026  0.4359347
## 45   0.30334 -0.3872 -0.11620406 -0.3450971 -0.2001922 -0.5579218
## 46   0.49568 -0.6491  0.09269456  0.0113337 -0.2479797  0.4463323
## 47   0.50134 -0.5245  0.17770348 -0.2079269  0.3535956 -0.3285473
## 48   0.42896 -0.4036 -0.08251409 -0.2863652 -0.1532384  0.3972309
## 49   0.64441 -0.2744 -0.00181107 -0.4862905  0.3800209  0.5085030
## 50   0.24473 -0.5326  0.13377237 -0.0969937 -0.4387219 -0.1296543
## 51   0.02692 -0.6685  0.05900716  0.1139970 -0.9763032 -0.3254478
## 52   0.26895 -0.6579  0.09892542 -0.0086692 -0.1716617  0.3125284
## 53   0.57888 -0.2664 -0.01985485 -0.5155828  0.3460181  0.4894294
## 54   0.60339 -0.2725 -0.00987070 -0.5014082  0.3674332  0.4520800
## 55   0.46991 -0.4055 -0.07411978 -0.2706858 -0.1400328 -0.3978772
## 56  -0.28239 -0.5085 -0.48545850  0.0606531  0.2255000  0.0249840
## 57  -0.34912 -0.8456 -0.29733229  0.5197827  0.0197029  0.0677022
## 58  -0.29694 -0.8119 -0.34228070  0.4449507 -0.0968054  0.1112188
## 59  -0.20348 -0.8393 -0.26550358  0.5453362  0.1230431  0.1535037
## 60  -0.20120 -0.8287 -0.32355725  0.5011482 -0.0746135  0.1235329
## 61  -0.62053 -0.8253 -0.35967030  0.4195148 -0.1203076 -0.0478118
## 62   0.38127  0.1873 -0.44254184  0.1842747 -0.2957415 -1.1152300
## 63   0.33717  0.1675 -0.45863288  0.2061793 -0.3631496  0.3991233
## 64   0.38509  0.1836 -0.43885286  0.1874734 -0.2838684 -1.1771015
## 65   0.33503  0.1758 -0.51634336  0.1657078 -0.5759025  0.3871496
## 66   0.38598  0.1888 -0.44215918  0.1815114 -0.2796088 -1.0985713
## 67   0.41097  0.1878 -0.43916957  0.1844034 -0.2645643  0.5457172
## 68   0.33448  0.1769 -0.51725785  0.1646312 -0.5774763  0.4178067
## 69   0.35607  0.1766 -0.51192332  0.1733880 -0.5637129  0.4882285
## 70   0.34055  0.3277 -0.68839195  0.0743787 -0.7174154  0.3377364
## 71   0.46423  0.2054 -0.48078905  0.1455334 -0.4209441 -1.0525845
## 72   0.26834 -0.7831 -0.20997141  0.5254564  0.3476506 -1.3180021
## 73   0.19731 -0.8764 -0.18202065  0.7133561  0.2675528  0.3847154
## 74   0.28397 -0.5710 -0.37802158  0.3224143  0.3842157  0.3478351
## 75   0.05877 -0.9829 -0.24117354  0.8923640 -0.1487131 -0.5964307
## 76   0.31632 -0.8109 -0.13943875  0.6167037  0.5433739  0.5573625
## 77   0.26053 -0.5719 -0.38149849  0.3178346  0.3680506  0.2963903
## 78   0.24900 -0.8344 -0.17064910  0.6470080  0.4059216 -1.2083557
## 79   0.26571 -0.5888 -0.37882844  0.3500144  0.3354410 -0.5066440
## 80  -0.00298 -0.8497 -0.20604579  0.7681608 -0.3670697 -1.0770898
## 81   0.27245 -0.7796 -0.21010633  0.5221928  0.3650440  0.4535574
## 82   0.18001 -0.8674 -0.19181778  0.6943084  0.2710980  0.3478446
## 83   0.18692 -0.6093 -0.40030656  0.3721828  0.2184549  0.2269844
## 84   0.18637 -0.6117 -0.39826504  0.3758200  0.2173339  0.2462332
## 85   0.21997 -0.8751 -0.17857989  0.7183895  0.2825038  0.3121944
## 86   0.31573 -0.8020 -0.14781273  0.6038292  0.5382539  0.4333352
## 87   0.19416 -0.8480 -0.24385392  0.6354120  0.1009927  0.3579172
## 88   0.26836 -0.8760 -0.16906841  0.7305728  0.3197321  0.2863619
## 89   0.23743 -0.5739 -0.38313301  0.3158818  0.3553921  0.2824523
## 90   0.30562 -0.5438 -0.35859180  0.2744275  0.4911935  0.3435024
## 91   0.19259 -0.8709 -0.18961655  0.7047358  0.2467896  0.3395287
## 92  -0.49490 -0.7999 -0.38303276  0.3738962 -0.1735451  0.0093066
## 93  -0.39460 -0.8403 -0.30833067  0.5030523 -0.0049017  0.0489276
## 94  -0.26141 -0.8489 -0.27864668  0.5499820  0.0670331  0.1187442
## 95  -0.14947 -0.8694 -0.25659953  0.6158330  0.0766759 -1.5333630
## 96  -0.29940 -0.8562 -0.29280060  0.5547247  0.0092643  0.0810878
## 97  -0.58898 -0.8527 -0.33844858  0.6389108 -0.7754391  0.2118162
## 98  -0.43164 -0.6948 -0.55864341  0.3835224 -0.4181953  0.0294334
## 99  -0.17369 -0.8154 -0.25463645  0.5095732  0.2034824 -1.5378200
## 100 -0.18632 -0.8298 -0.32254321  0.5080320 -0.0911198  0.1964618
## 101 -0.69154  0.4138 -0.19389551 -0.6423244  0.4544509 -0.0482401
## 102 -0.67627  0.3664 -0.14838302 -0.5298781  0.5157452 -0.0713981
## 103 -0.61035  0.3638 -0.11344423 -0.5061802  0.6242461 -0.0524947
## 104 -0.55393  0.3801 -0.10646838 -0.5195751  0.6740255  0.1581004
## 105 -0.51685  0.3541 -0.10397028 -0.4629184  0.6309226 -0.0732986
## 106 -0.50244  0.3540 -0.10089658 -0.4526516  0.6206843  0.0048946
## 107 -0.54305  0.3802 -0.16723251 -0.5425215  0.4517838  0.0523044
## 108 -0.56998  0.3767 -0.16856465 -0.5412739  0.4269021 -0.0794864
## 109 -0.50638  0.3820 -0.10075085 -0.5127575  0.7051616 -0.0222836
## 110 -0.52034  0.3530 -0.10213714 -0.4576637  0.6246107  0.0562494
## 111 -0.52160  0.3506 -0.10174076 -0.4560380  0.6174290  0.0326903
## 112 -0.59338  0.3795 -0.17601080 -0.5521409  0.4079333 -0.0374668
## 113 -0.59352  0.3779 -0.17390050 -0.5487379  0.4098925 -0.0561353
## 114 -0.54497  0.3890 -0.10001480 -0.5316154  0.6954813  0.0593755
## 115 -0.58595  0.3589 -0.11963305 -0.4859376  0.5875352  0.0487897
## 116 -0.53113  0.3826 -0.10434905 -0.5167086  0.6882481  0.0103063
## 117 -0.46678  0.3762 -0.08934115 -0.4913138  0.7146768  0.1623066
## 118 -0.44537  0.3739 -0.08248553 -0.4800018  0.7278856  0.0882793
## 119 -0.45987  0.3487 -0.08997787 -0.4340413  0.6377728  0.0310829
## 120 -0.40607  0.3714 -0.07355976 -0.4623327  0.7369059  0.0329277
## 121 -0.42318  0.3741 -0.07900487 -0.4744981  0.7392268  0.1208888
## 122 -0.46474  0.3783 -0.08911868 -0.4925146  0.7247661  0.1509402
## 123 -0.46664  0.3764 -0.08673639 -0.4874704  0.7221992  0.1308796
## 124 -0.49048  0.3728 -0.08836553 -0.4888023  0.7043214  0.0465747
## 125 -0.53786  0.3857 -0.09467656 -0.5282120  0.7208874  0.0218218
## 126 -0.66685  0.3208 -0.11509106 -0.3741550  0.1472574  0.0913330
## 127 -0.52714  0.3754 -0.15978788 -0.5263474  0.4470207  0.0701855
## 128  0.35696  0.1938  0.05835095  0.0671613  0.6790565  0.4488227
## 129  0.36357  0.2000  0.05590987  0.0585031  0.7023375  0.3963692
## 130  0.35818  0.2226 -0.00003005 -0.0085747  0.5334181  0.5226288
## 132  0.41119  0.1999  0.06356816  0.0683470  0.7345542 -1.1240865
## 133  0.32784  0.3244  0.09002245 -0.0384778  0.4569765 -0.0636149
## 134  0.42165  0.2057  0.07205467  0.0585535  0.7586365  0.5265838
## 135  0.41187  0.1987  0.06559147  0.0710324  0.7389910  0.4443321
## 136  0.40955  0.1970  0.06441586  0.0709524  0.7265590  0.3900800
## 137  0.38826  0.3518  0.11504704 -0.0736557  0.5684225  0.8752436
## 138  0.38134  0.1924  0.06366246  0.0740312  0.6975534  0.4800753
## 139  0.38877  0.1989  0.06088451  0.0642774  0.7227237  0.4690289
## 140  0.33188  0.4604 -0.16213848 -0.2827219  0.6404552  0.3368191
## 141  0.37791  0.4648 -0.15749331 -0.2766340  0.6698367 -1.2528258
## 142  0.37766  0.4625 -0.15598473 -0.2740230  0.6683204 -1.2677078
## 143  0.38153  0.3605  0.10747797 -0.0965951  0.6043477 -0.9058399
## 144  0.21756  0.2349  0.04509500  0.1113724  0.1117213 -0.9612884
## 145  0.45267  0.1942  0.07504545  0.0885892  0.7491823 -1.2006660
## 146  0.40315  0.2196  0.00782823  0.0035905  0.5532760  0.5480511
## 147  0.37939  0.3569  0.11045477 -0.0903694  0.5991833  0.7273726
## 148  0.43029  0.1976  0.06806774  0.0776880  0.7354471 -1.0901295
## 149  0.35843  0.2232 -0.00190638 -0.0116316  0.5303017 -1.1648683
## 150  0.42858  0.3592  0.11584695 -0.0852008  0.6344079 -0.1112568
## 151  0.67016  0.3618  0.16123082 -0.1832122  1.3807221 -0.1572580
## 152  0.09075  0.1934  0.00810888  0.1591185 -0.0837473  0.5252666
## 153  0.46726  0.3143  0.04133101 -0.1631489  0.8707812 -1.1184988
## 154 -0.52425  0.3757 -0.16096584 -0.5301527  0.4521482  0.1261773
## 155 -0.75298  0.3449 -0.14247553 -0.3422752 -0.1519807  0.1684962
## 156 -0.70315  0.3798 -0.12484787 -0.3973880 -0.0165294  0.3336867
## 157  0.29844  0.2259 -0.01326760 -0.0342668  0.5171957  0.4866559
## 158  0.58194  0.3881  0.08684670 -0.2835089  1.1783025  0.7199798
## 159  0.27643  0.2272 -0.01820654 -0.0422012  0.5053990  0.4237042
## 160  0.37593  0.3547  0.11065734 -0.0875351  0.5863825  0.8398935
## 161  0.58176  0.2647 -0.11574326 -0.6298547 -1.1885344  0.5182059
## 162  0.66692  0.2632 -0.10025539 -0.6028618 -1.1525398  0.4912875
## 163  0.88557  0.3117 -0.03986374 -0.6421959 -0.8657638 -0.9351448
## 164  0.74332  0.1703 -0.11342082 -0.4039138 -1.4007078  0.5870841
## 165  0.94824  0.3088 -0.02911489 -0.6223203 -0.8477694 -0.9339759
## 166  0.94384  0.3058 -0.02713934 -0.6160966 -0.8599786 -0.8828733
## 167  0.63573  0.1894 -0.12198436 -0.4672983 -1.4036911  0.5918133
## 168  0.76373  0.2801 -0.07495488 -0.6107247 -1.0599116  0.6178046
## 169 -0.27704  1.5151 -2.64803806  1.5348601 -0.3480006  0.2231687
## 170 -0.27329  1.5192 -2.65097499  1.5275515 -0.3376124  0.1078557
## 171 -0.25636  1.5288 -2.64189621  1.5139508 -0.2874740  0.2370105
## 172 -0.29694  1.5307 -2.64950054  1.4992170 -0.2977527  0.2901946
## 173  0.99188  0.3542 -0.00789814 -0.6990131 -0.6791011  0.7817843
## 174  1.01616  0.3107 -0.01919713 -0.6076462 -0.8084664  0.9048440
## 175  0.86220  0.2731 -0.11326650 -0.5974164 -1.2333812  0.7669229
## 176  0.90306  0.3104 -0.03821229 -0.6361374 -0.8741995 -0.8757004
## 177  0.95451  0.2487 -0.04081656 -0.5029892 -1.0217047  0.7308182
## 178  1.01101  0.3036 -0.01399270 -0.5957388 -0.8227386 -0.8639895
## 179  1.03582  0.3290 -0.06601442 -0.6623473 -0.9529923 -0.7315716
## 180 -0.53897  0.3328  0.74290651  0.5329582 -0.3952038  0.0540893
## 181 -0.53598  0.3349  0.74267217  0.5304012 -0.3837777 -0.0476748
## 182 -0.67303  0.3692  0.71650825  0.4285943 -0.3671464 -0.0956663
## 183  0.54033  0.3639  0.92205260  0.7130098  0.1392670 -1.0254803
## 184  0.55900  0.3045  0.97284574  0.8573389  0.1985471  0.7126631
## 185  0.61086  0.2745  0.98080754  0.9278814  0.1443735  0.6261817
## 186  0.76722  0.3353  1.00256736  0.8358545  0.3237594 -0.9179649
## 187  0.56814  0.2753  0.97184890  0.9126374  0.1228861  0.5880553
## 188  0.57449  0.2787  0.97135776  0.9072854  0.1450503  0.6570990
## 189  0.58617  0.3389  0.98624268  0.7959299  0.3186234 -0.9868713
## 190  0.52716  0.2811  0.96064934  0.8924676  0.1103479  0.4960420
## 191  0.43666  0.2834  0.94435734  0.8678832  0.0602220  0.5142399
## 192  0.48115  0.2841  0.95206742  0.8799215  0.0859001  0.2745040
## 193  0.49421  0.5411  0.74518208  0.5560557  0.0511396  0.5414325
## 194 -0.71803  0.3808  0.64851918  0.3708105 -0.6001592  0.1604306
## 195 -0.80678  0.6101  0.47423742  0.1058311 -0.5559662 -0.1547960
## 196 -0.78712  0.6226  0.47897465  0.0835721 -0.5042432 -0.2418915
## 197 -0.67102  0.3533  0.71700233  0.4595968 -0.4116724 -0.0513287
## 198 -0.77189  0.3463  0.69310477  0.4462992 -0.4905532 -0.0077506
## 199 -0.71601  0.3689  0.71066216  0.4183204 -0.3834558  0.0002466
## 200 -0.74272  0.6227  0.48635835  0.0952086 -0.4806928 -0.1992138
## 201 -0.75776  0.3693  0.63681814  0.3807483 -0.6646081 -0.0234701
## 202 -0.73712  0.3832  0.64378117  0.3610966 -0.6016185 -0.0363704
## 203 -0.87458  0.2458  0.64635790  0.6194814 -0.8619230 -0.2248306
## 204 -0.80336  0.3815  0.63592837  0.3483771 -0.6306851 -0.0253304
## 205 -0.75488  0.3530  0.70495169  0.4370346 -0.4390432 -0.0728607
## 206 -0.63589  0.6190  0.50724432  0.1311306 -0.4345183 -0.0701689
## 207 -0.70151  0.6154  0.49889048  0.1173341 -0.4670537 -0.1634384
## 208 -0.71895  0.6201  0.49240980  0.1037014 -0.4642761 -0.1419400
## 209 -0.68106  0.6157  0.50236037  0.1240150 -0.4599138 -0.0497915
## 210 -0.68640  0.3422  0.71035498  0.4759087 -0.4543869 -0.1040708
## 211 -0.66092  0.3457  0.71143942  0.4752919 -0.4339715 -0.0043869
## 212 -0.61146  0.5975  0.51599497  0.1750835 -0.4662550 -0.0813187
## 213 -0.56114  0.3475  0.73995571  0.4966764 -0.3563716  0.0477053
## 214 -0.52799  0.3338  0.74436647  0.5275368 -0.3611230  0.0812733
## 215 -0.61465  0.6133  0.51547755  0.1448668 -0.4251426 -0.0399757
## 216 -0.60685  0.3475  0.73273969  0.4861865 -0.3826482 -0.0073943
## 217  0.43761  0.3108  0.88294178  0.7878133 -0.0970118  0.5780239
## 218  0.57415  0.3695  0.95087072  0.7641316  0.2028820 -1.0709812
## 219  0.47953  0.2802  0.95413471  0.8844198  0.0792762  0.5647443
## 220  0.67390  0.2971  0.99754379  0.8936990  0.2680709  0.5835910
## 
## 
## Biplot scores for constraining variables
## 
##                            dbRDA1   dbRDA2   dbRDA3   dbRDA4  dbRDA5 MDS1
## depthM                   -0.85366  0.18480  0.05068  0.28234 -0.2481    0
## BO22_curvelmean_ss        0.04812 -0.55958 -0.57193  0.45956 -0.2519    0
## BO22_lightbotmean_bdmean  0.46906  0.01103  0.04511 -0.36336 -0.7563    0
## BO22_tempmean_bdmean      0.18907  0.18809  0.57097  0.08928 -0.7061    0
## BO22_ppmean_bdmean       -0.24726 -0.78343 -0.24709  0.11912  0.4653    0

The model explains some of the genetic variation we see. Now we can run forward selection to find the best model that explains the data without over-fitting.

set.seed(092)
bestDbrda = ordiR2step(dbrda0, dbrda1, permutations = how(nperm = 99999), R2permutations = 9999, direction = "forward")
## Step: R2.adj= 0 
## Call: sintMa ~ 1 
##  
##                                  R2.adjusted
## <All variables>               0.017110705395
## + depthM                      0.014402973316
## + BO22_lightbotmean_bdmean    0.002528947465
## + BO22_ppmean_bdmean          0.001600418609
## + BO22_curvelmean_ss          0.000092235515
## <none>                        0.000000000000
## + Condition(sym)              0.000000000000
## + Condition(PC1 + PC2 + PC3)  0.000000000000
## + BO22_tempmean_bdmean       -0.000001733873
## 
##          Df    AIC      F  Pr(>F)    
## + depthM  1 607.53 4.1857 0.00001 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Step: R2.adj= 0.01440297 
## Call: sintMa ~ depthM 
##  
##                              R2.adjusted
## <All variables>               0.01711071
## + BO22_ppmean_bdmean          0.01645990
## + BO22_tempmean_bdmean        0.01574441
## + Condition(sym)              0.01571710
## + BO22_lightbotmean_bdmean    0.01480829
## + BO22_curvelmean_ss          0.01479496
## <none>                        0.01440297
## + Condition(PC1 + PC2 + PC3)  0.01272365
## 
##                      Df    AIC      F  Pr(>F)   
## + BO22_ppmean_bdmean  1 608.07 1.4538 0.00975 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Step: R2.adj= 0.0164599 
## Call: sintMa ~ depthM + BO22_ppmean_bdmean 
##  
##                              R2.adjusted
## + Condition(sym)              0.01772227
## + BO22_curvelmean_ss          0.01764181
## <All variables>               0.01711071
## + BO22_tempmean_bdmean        0.01698154
## <none>                        0.01645990
## + BO22_lightbotmean_bdmean    0.01629163
## + Condition(PC1 + PC2 + PC3)  0.01462205
bestDbrda$anova
##                        R2.adj Df    AIC      F  Pr(>F)    
## + depthM             0.014403  1 607.53 4.1857 0.00001 ***
## + BO22_ppmean_bdmean 0.016460  1 608.07 1.4538 0.00975 ** 
## <All variables>      0.017111                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Depth and Primary productivity are the environmental variables that best explain the variation (although only 2.18%).

model = envData2 %>% dplyr::select("Depth" = depthM, "PP" = BO22_ppmean_bdmean, sym, PC1, PC2, PC3)

sintDbrda = dbrda(sintMa ~ Depth + PP + Condition(sym) + Condition(PC1 + PC2 + PC3), data = model)

dbrdaVarPart = varpart(as.dist(sintMa), ~ Depth, ~ PP, data = model)
dbrdaDepth = dbrda(sintMa ~ Depth + Condition(sym) + Condition(PC1 + PC2 + PC3), data = model)
dbrdaPP = dbrda(sintMa ~ PP + Condition(sym) + Condition(PC1 + PC2 + PC3), data = model)


dbrdaVarPart
## 
## Partition of squared Unknown user-supplied distance in dbRDA 
## 
## Call: varpart(Y = as.dist(sintMa), X = ~Depth, ~PP, data = model)
## 
## Explanatory tables:
## X1:  ~Depth
## X2:  ~PP 
## 
## No. of explanatory tables: 2 
## Total variation (SS): 16.112 
## No. of observations: 219 
## 
## Partition table:
##                      Df R.squared Adj.R.squared Testable
## [a+c] = X1            1   0.01892       0.01440     TRUE
## [b+c] = X2            1   0.00618       0.00160     TRUE
## [a+b+c] = X1+X2       2   0.02548       0.01646     TRUE
## Individual fractions                                    
## [a] = X1|X2           1                 0.01486     TRUE
## [b] = X2|X1           1                 0.00206     TRUE
## [c]                   0                -0.00046    FALSE
## [d] = Residuals                         0.98354    FALSE
## ---
## Use function 'dbrda' to test significance of fractions of interest
set.seed(003)
anova(sintDbrda)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = sintMa ~ Depth + PP + Condition(sym) + Condition(PC1 + PC2 + PC3), data = model)
##           Df SumOfSqs      F Pr(>F)    
## Model      2   0.3937 2.7126  0.001 ***
## Residual 212  15.3840                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
set.seed(002)
anova(dbrdaDepth)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = sintMa ~ Depth + Condition(sym) + Condition(PC1 + PC2 + PC3), data = model)
##           Df SumOfSqs      F Pr(>F)    
## Model      1   0.2924 4.0218  0.001 ***
## Residual 213  15.4852                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
set.seed(001)
anova(dbrdaPP)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = sintMa ~ PP + Condition(sym) + Condition(PC1 + PC2 + PC3), data = model)
##           Df SumOfSqs      F Pr(>F)  
## Model      1   0.0974 1.3235  0.021 *
## Residual 213  15.6802                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Now create the best model and prepare to plot

sintRdaVar = round(sintDbrda$CA$eig/sum(sintDbrda$CA$eig)*100, 1)
head(sintRdaVar)
## MDS1 MDS2 MDS3 MDS4 MDS5 MDS6 
##  7.6  3.5  2.9  1.6  1.3  1.1
sintRdaVarFit = round(sintDbrda$CCA$eig/sum(sintDbrda$CCA$eig)*100, 1)
head(sintRdaVarFit)
## dbRDA1 dbRDA2 
##   79.7   20.3
sintI2P = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66,68,131,164,166,209,211),] %>% dplyr::select("sample" = tubeID, "pop" = site, "depth" = depthZone)

sintI2P$popdepth = paste(sintI2P$pop, sintI2P$depth)

sintRdaPoints = as.data.frame(scores(sintDbrda, choices = c(1,2,3))$sites)
# sintRdaPoints = as.data.frame(scores(sintDbrda))
sintRdaPoints$sample = sintI2P$sample
head(sintRdaPoints)
##       dbRDA1      dbRDA2        MDS1 sample
## 1 -0.9345180  0.38290994 0.170482268 SFK001
## 2 -0.1914385 -1.28914359 0.080333585 SFK002
## 3 -0.4439793 -1.05523131 0.081450615 SFK003
## 4 -0.6478891 -0.03986449 0.136663200 SFK004
## 5 -0.6346500 -0.30248220 0.002683636 SFK005
## 6 -0.7263526  0.16643854 0.038146344 SFK006
sintDbrdaData = sintI2P %>% left_join(sintRdaPoints) %>% left_join((pcangsd %>% dplyr::select(sample, "K" = cluster)))
## Joining with `by = join_by(sample)`
## Joining with `by = join_by(sample)`
head(sintDbrdaData)
##   sample           pop      depth                 popdepth     dbRDA1      dbRDA2
## 1 SFK001 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.9345180  0.38290994
## 2 SFK002 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.1914385 -1.28914359
## 3 SFK003 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.4439793 -1.05523131
## 4 SFK004 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.6478891 -0.03986449
## 5 SFK005 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.6346500 -0.30248220
## 6 SFK006 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.7263526  0.16643854
##          MDS1    K
## 1 0.170482268 Blue
## 2 0.080333585 Teal
## 3 0.081450615 Teal
## 4 0.136663200 Blue
## 5 0.002683636 Blue
## 6 0.038146344 Blue
tail(sintDbrdaData)
##     sample        pop      depth              popdepth     dbRDA1     dbRDA2
## 214 SFK215 Upper Keys Mesophotic Upper Keys Mesophotic -0.6080761 1.26693886
## 215 SFK216 Upper Keys Mesophotic Upper Keys Mesophotic -0.4717725 0.08138907
## 216 SFK217 Upper Keys    Shallow    Upper Keys Shallow -0.6446541 0.59855828
## 217 SFK218 Upper Keys    Shallow    Upper Keys Shallow  2.1176486 1.87153237
## 218 SFK219 Upper Keys    Shallow    Upper Keys Shallow -0.6295469 0.32766360
## 219 SFK220 Upper Keys    Shallow    Upper Keys Shallow -0.3172208 0.29777902
##            MDS1       K
## 214 -0.02252615    Blue
## 215 -0.04609176 Admixed
## 216  0.45776329    Blue
## 217 -1.16359211   Green
## 218  0.43610347    Blue
## 219  0.44348830    Blue
envLoad = as.data.frame(sintDbrda$CCA$biplot)
envLoad$var = row.names(envLoad)

sintDbrdaData$depth = factor(sintDbrdaData$depth)
sintDbrdaData$depth = factor(sintDbrdaData$depth, levels(sintDbrdaData$depth)[c(2,1)])
sintDbrdaData$pop = factor(sintDbrdaData$pop)
sintDbrdaData$pop = factor(sintDbrdaData$pop, levels(sintDbrdaData$pop)[c(4, 1, 3, 2)])

head(sintDbrdaData)
##   sample           pop      depth                 popdepth     dbRDA1      dbRDA2
## 1 SFK001 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.9345180  0.38290994
## 2 SFK002 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.1914385 -1.28914359
## 3 SFK003 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.4439793 -1.05523131
## 4 SFK004 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.6478891 -0.03986449
## 5 SFK005 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.6346500 -0.30248220
## 6 SFK006 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.7263526  0.16643854
##          MDS1    K
## 1 0.170482268 Blue
## 2 0.080333585 Teal
## 3 0.081450615 Teal
## 4 0.136663200 Blue
## 5 0.002683636 Blue
## 6 0.038146344 Blue

Now plot the dbRDA

sintDbrdaPlotA = ggplot() +
  geom_hline(yintercept = 0, color = "gray90", size = 0.5) +
  geom_vline(xintercept = 0, color = "gray90", size = 0.5) +
  geom_point(data = sintDbrdaData, aes(x = dbRDA1, y = dbRDA2, fill = K, shape = depth), color = "black", size = 2, alpha = 1) +
  scale_shape_manual(values = c(21, 23), name = "Depth Zone") +
  geom_segment(data = envLoad, aes(x = 0, y = 0, xend = dbRDA1, yend = dbRDA2), color = "#F5065B", arrow = arrow(length = unit(0.15, "cm"), type = "open"), size = 0.65) +
  geom_text(data = envLoad[1,], aes(x = dbRDA1-0.14, y = dbRDA2-0.08, label = var), color = "#F5065B", size = 3, fontface = "bold") +
   geom_text(data = envLoad[2,], aes(x = dbRDA1-0.03, y = dbRDA2-0.1, label = var), color = "#F5065B", size = 3, fontface = "bold") +
  scale_fill_manual(values = kColPal, name = "Lineage") +
  scale_color_manual(values = kColPal, name = "Lineage", guide = NULL) +
  labs(title = expression(italic("S. intersepta")), x = paste0("dbRDA 1 (", sintRdaVarFit[1], "% [",sintRdaVar[1], "%])"), y = paste0("dbRDA 2 (", sintRdaVarFit[2], "% [",sintRdaVar[2], "%])")) +
  guides(shape = guide_legend(override.aes = list(size = 3, stroke = 0.5, alpha = 1), order = 2), fill = guide_legend(override.aes = list(shape = 22, size = 4, alpha = NA, fill = kColPal), order = 1))+
  theme_bw()

sintDbrdaPlot = sintDbrdaPlotA +
  theme(plot.title = element_text(color = "black", size = 12),
        axis.title.x = element_text(color = "black", size = 10),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.title.y = element_text(color = "black", size = 10),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank(),
        legend.spacing = unit(-5, "pt"),
        legend.key.size = unit(5, "pt"),
        legend.position = c(1.1495, 0.5),
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 8),
        panel.border = element_rect(color = "black", size = 1),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

sintDbrdaPlot


Genetic connectivity

Checking deviance among model runs from BayesAss we ran on HPC

# fileList = substr(list.files("../data/snps/bayesAss/", "BA3trace.*.txt$"),1,10)
fileList = substr(list.files("../data/snps/BA3/", "BA3trace.*.txt$"),1,11)

bayesian_deviance <- function(trace, burnin = 0, sampling.interval = 0){
  if(burnin == 0) stop('No burnin specified')
  if(sampling.interval == 0) stop('No sampling interval specified')
  range <- (trace$State > burnin & trace$State %% sampling.interval == 0)
  D <- -2*mean(trace$LogProb[range])
  return(D)
}

for(i in 1:length(fileList)){
  assign(fileList[i], read.delim(paste("../data/snps/BA3/", fileList[i], ".txt", sep = ""))) %>% dplyr::select(-last_col())
  print(paste(fileList[i], bayesian_deviance(get(fileList[i]), burnin = 10000000, sampling.interval = 100)))
}
## [1] "BA3trace.01 1078700.5909"
## [1] "BA3trace.02 1078407.0012"
## [1] "BA3trace.03 1078727.4004"
## [1] "BA3trace.04 1078794.1071"
## [1] "BA3trace.05 1078778.0004"
## [1] "BA3trace.06 1078681.4417"
## [1] "BA3trace.07 1078710.4509"
## [1] "BA3trace.08 1078817.9814"
## [1] "BA3trace.09 1078717.2801"
## [1] "BA3trace.10 1078433.2838"
# [1] "BA3trace.01 1078700.5909"
# [1] "BA3trace.02 1078407.0012"
# [1] "BA3trace.03 1078727.4004"
# [1] "BA3trace.04 1078794.1071"
# [1] "BA3trace.05 1078778.0004"
# [1] "BA3trace.06 1078681.4417"
# [1] "BA3trace.07 1078710.4509"
# [1] "BA3trace.08 1078817.9814"
# [1] "BA3trace.09 1078717.2801"
# [1] "BA3trace.10 1078433.2838"

All traces have similar deviance (this is good!). Using the trace with the lowest deviance (BA3trace.02.txt, in this case)

bayesAss = read.delim("../data/snps/BA3/BA3trace.02.txt") %>% filter(State > 10000000) %>% dplyr::select(-State, -LogProb, -X)

baMean = bayesAss %>% summarise(across(everything(), list(mean))) %>% t() %>% as_tibble() %>% rename(., mean=V1) %>% mutate(pops = colnames(bayesAss))

baSumm = bayesAss %>% summarise(across(everything(), list(median))) %>% t() %>% as.data.frame() %>% rename(., median=V1) %>% mutate(pops = baMean$pops, mean = round(baMean$mean, 3)) %>% relocate(median, .after = mean)

baSumm$median = round(baSumm$median, 3)

baHpd =as.data.frame(t(sapply(bayesAss, emp.hpd)))
colnames(baHpd) = c("hpdLow", "hpdHigh")
baHpd$pops = rownames(baHpd)

ESS = as.data.frame(sapply(bayesAss, ESS))
colnames(ESS) = "ESS"

baSumm = baSumm %>% left_join(baHpd)
## Joining with `by = join_by(pops)`
baSumm$hpdLow = round(baSumm$hpdLow, 3)
baSumm$hpdHigh = round(baSumm$hpdHigh, 3)
baSumm$ESS = ESS$ESS

### FROM BAYESASS: ###
## Population Index -> Population Label:
## 0->TortugasBank_Mesophotic 1->TortugasBank_Shallow
## 2->RileysHump_Mesophotic 3->RileysHump_Shallow
## 4->LowerKeys_Mesophotic 5->LowerKeys_Shallow
## 6->UpperKeys_Shallow 7->UpperKeys_Mesophotic

popi = rep(c("Tortugas Bank\nMesophotic", "Tortugas Bank\nShallow", "Riley's Hump\nMesophotic", "Riley's Hump\nShallow", "Lower Keys\nMesophotic", "Lower Keys\nShallow", "Upper Keys\nShallow", "Upper Keys\nMesophotic"), each = 8)

popj = rep(c("Tortugas Bank\nMesophotic", "Tortugas Bank\nShallow", "Riley's Hump\nMesophotic", "Riley's Hump\nShallow", "Lower Keys\nMesophotic", "Lower Keys\nShallow", "Upper Keys\nShallow", "Upper Keys\nMesophotic"), times = 8)

baSumm = baSumm %>% mutate(pop.i = popi, pop.j = popj) %>% relocate(c(pop.i, pop.j), .after = pops) %>% dplyr::select(-pops)

baSumm$pop.i = factor(baSumm$pop.i)
baSumm$pop.i = factor(baSumm$pop.i, levels = levels(baSumm$pop.i)[c(8, 2, 6, 4, 7, 1, 5, 3)])

baSumm$pop.j = factor(baSumm$pop.j)
baSumm$pop.j = factor(baSumm$pop.j, levels = levels(baSumm$pop.j)[c(8, 2, 6, 4, 7, 1, 5, 3)])

baSumm$site.i = word(baSumm$pop.i, 1, sep = "\n")
baSumm$site.i = factor(baSumm$site.i)
baSumm$site.i = factor(baSumm$site.i, levels = levels(baSumm$site.i)[c(4, 1, 3, 2)])

baSumm$site.j = word(baSumm$pop.j, 1, sep = "\n")
baSumm$site.j = factor(baSumm$site.j)
baSumm$site.j = factor(baSumm$site.j, levels = levels(baSumm$site.j)[c(4, 1, 3, 2)])

baSumm$depth.i = word(baSumm$pop.i, 2, sep = "\n")
baSumm$depth.i = factor(baSumm$depth.i)
baSumm$depth.i = factor(baSumm$depth.i, levels = levels(baSumm$depth.i)[c(2, 1)])

baSumm$depth.j = word(baSumm$pop.j, 2, sep = "\n")
baSumm$depth.j = factor(baSumm$depth.j)
baSumm$depth.j = factor(baSumm$depth.j, levels = levels(baSumm$depth.j)[c(2, 1)])
#All sites (excluding self retention)
baMeans = baSumm %>% filter(pop.i != pop.j) %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.))) %>% mutate(dataset = "Global")

#mesophotic sources
baMeans = baSumm %>% filter(pop.i != pop.j, depth.j == "Mesophotic") %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.))) %>% mutate(dataset = "Mesophotic Source") %>% bind_rows(baMeans, .)

#shallow sources
baMeans = baSumm %>% filter(pop.i != pop.j, depth.j == "Shallow") %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.))) %>% mutate(dataset = "Shallow Source") %>% bind_rows(baMeans, .)

#mesophotic sinks
baMeans = baSumm %>% filter(pop.i != pop.j, depth.i == "Mesophotic") %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.)))  %>% mutate(dataset = "Mesophotic Sink") %>% bind_rows(baMeans, .)

#shallow sinks
baMeans = baSumm %>% filter(pop.i != pop.j, depth.i == "Shallow") %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.)))  %>% mutate(dataset = "Shallow Sink") %>% bind_rows(baMeans, .)

#mesophotic -> shallow
baMeans = baSumm %>% filter(pop.i != pop.j, depth.j == "Mesophotic", depth.i == "Shallow") %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.))) %>% mutate(dataset = "Mesophotic -> Shallow") %>% bind_rows(baMeans, .)

#mesophotic -> mesophotic
baMeans = baSumm %>% filter(pop.i != pop.j, depth.j == "Mesophotic", depth.i == "Mesophotic") %>% summarise(mean = mean(mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.))) %>% mutate(dataset = "Mesophotic -> Mesophotic") %>% bind_rows(baMeans, .)

#shallow -> mesophotic
baMeans = baSumm %>% filter(pop.i != pop.j, depth.j == "Shallow", depth.i == "Mesophotic") %>% summarise(mean = mean(.$mean), sd = sd(.$mean), se = sd(.$mean)/sqrt(nrow(.))) %>% mutate(dataset = "Shallow -> Mesophotic") %>% bind_rows(baMeans, .)

#shallow -> shallow
baMeans = baSumm %>% filter(pop.i != pop.j, depth.j == "Shallow", depth.i == "Shallow") %>% summarise(mean = round(mean(.$mean), 5), sd = round(sd(.$mean), 5), se = round(sd(.$mean)/sqrt(nrow(.)), 3)) %>% mutate(dataset = paste("Shallow -> Shallow")) %>% bind_rows(baMeans, .) %>% relocate(dataset, .before = mean) %>% as.data.frame()

baMeans[,c(2:4)] = baMeans[,c(2:4)] %>% round(4)

baMeans
##                    dataset   mean     sd     se
## 1                   Global 0.0410 0.0572 0.0076
## 2        Mesophotic Source 0.0670 0.0725 0.0137
## 3           Shallow Source 0.0151 0.0042 0.0008
## 4          Mesophotic Sink 0.0378 0.0648 0.0122
## 5             Shallow Sink 0.0443 0.0495 0.0094
## 6    Mesophotic -> Shallow 0.0649 0.0579 0.0145
## 7 Mesophotic -> Mesophotic 0.0698 0.0912 0.0263
## 8    Shallow -> Mesophotic 0.0138 0.0049 0.0012
## 9       Shallow -> Shallow 0.0168 0.0025 0.0010
baMeansTabPub = baMeans %>%
  flextable() %>%
  flextable::compose(part = "header", j = "dataset", value = as_paragraph("Dataset")) %>%
  flextable::compose(part = "header", j = "mean", value = as_paragraph(as_i("m"))) %>%
  flextable::compose(part = "header", j = "sd", value = as_paragraph("SD")) %>%
  flextable::compose(part = "header", j = "se", value = as_paragraph("SEM")) %>%
  flextable::font(fontname = "Times New Roman", part = "all") %>%
  flextable::fontsize(size = 10, part = "all") %>%
  flextable::bold(part = "header") %>%
  flextable::align(align = "left", part = "all") %>%
  flextable::autofit()

table3 = read_docx()
table3 = body_add_flextable(table3, value = baMeansTabPub)
print(table3, target = "../tables/table3.docx")

baMeansTabPub
baSumm$mean = sprintf('%.3f', baSumm$mean)
baSumm$mean2 = baSumm$mean
baSumm$hpdLow = sprintf('%.3f', baSumm$hpdLow)
baSumm$hpdHigh = sprintf('%.3f', baSumm$hpdHigh)

baLabs = tibble(pop.i = unique(baSumm$pop.i), pop.j = unique(baSumm$pop.j))

migrateA = ggplot(data = baSumm, aes(pop.i, pop.j))+
  geom_tile(data = subset(baSumm, subset = baSumm$mean2>0.65), fill = "gray35", color = "white") +
  geom_segment(data = baSumm, aes(x = 0.4755, xend = -0.55, y = pop.j, yend = pop.j, color = pop.j), size = 14) +
  geom_segment(data = baSumm, aes(x = pop.i, xend = pop.i, y = 0.45, yend = -0.425, color = pop.i), size = 32) +
  scale_color_manual(values = flPal[c(1:4, 1:4)], guide = NULL) +

  guides(fill = guide_colorbar(ticks.colour = "black", barwidth = 1, barheight = 10, frame.colour = "black")) +
  # new_scale("fill") +
  geom_tile(data = subset(baSumm, subset = baSumm$mean<0.65), aes(fill = as.numeric(as.character(mean))), color = "white") +
  scale_fill_gradientn(colours = paletteer_c("viridis::mako", n = 10, direction = -1)[c(1:7)], limit = c(0,0.27), space = "Lab", name = expression(paste(italic("m"))), na.value = "transparent",  guide = "colourbar", values = c(0, 0.05, 0.1, 0.15, 0.2,0.5,0.75,1)) +
  # scale_fill_gradientn(colours = paletteer_d("khroma::smoothrainbow"), limit = c(0,0.27), space = "Lab", name = expression(paste(italic("m"))), na.value = "transparent",  guide = "colourbar", values = c(0, 0.05, 0.1, 0.15, 0.2,0.5,0.75,1)) +
  geom_text(data = baSumm, aes(x = pop.i, y = pop.j, label = paste(mean, "\n", sep = "")), color = ifelse(baSumm$mean > 0.6, "white", "gray5"), fontface = ifelse(as.numeric(baSumm$hpdLow)>0, "bold", "plain"), size = ifelse(as.numeric(baSumm$hpdLow)>0, 4.75, 4)) +
  geom_text(data = baSumm, aes(x = pop.i, y = pop.j, label = paste("\n(",hpdLow,"–",hpdHigh, ")", sep = "")), color = ifelse(baSumm$mean > 0.6, "white", "gray5"), size = 3.25) +
  
  geom_text(data = (baLabs %>% filter(pop.j %in% c("Tortugas Bank\nMesophotic", "Tortugas Bank\nShallow", "Riley's Hump\nMesophotic", "Riley's Hump\nShallow"))), x = -.02, aes(y = pop.j, label = pop.j), size = 3.75, color = "#FFFFFF", family = "sans") +
  geom_text(data = (baLabs %>% filter(!pop.j %in% c("Tortugas Bank\nMesophotic", "Tortugas Bank\nShallow", "Riley's Hump\nMesophotic", "Riley's Hump\nShallow"))), x = -.02, aes(y = pop.j, label = pop.j), size = 3.75, color = "#000000", family = "sans") +
  geom_text(data = (baLabs %>% filter(pop.i %in% c("Tortugas Bank\nMesophotic", "Tortugas Bank\nShallow", "Riley's Hump\nMesophotic", "Riley's Hump\nShallow"))), y = -.01, aes(x = pop.i, label = pop.i), size = 3.75, color = "#FFFFFF", family = "sans") +
  geom_text(data = (baLabs %>% filter(!pop.i %in% c("Tortugas Bank\nMesophotic", "Tortugas Bank\nShallow", "Riley's Hump\nMesophotic", "Riley's Hump\nShallow"))), y = -.01, aes(x = pop.i, label = pop.i), size = 3.75, color = "#000000", family = "sans") +
  
  labs(x = "Sink", y = "Source") +
  scale_y_discrete(limits = rev(levels(baSumm$pop.i))[c(1:8)], position = "left") +
  coord_cartesian(xlim = c(1, 8), ylim = c(1, 8), clip = "off") +
  theme_minimal()

migrate = migrateA + theme(
  axis.text.x = element_text(vjust = 1, size = 12, hjust = 0.5, color = NA),
  axis.text.y = element_text(size = 10, color = NA),
  axis.title.x = element_text(size = 14),
  axis.title.y = element_text(size = 14),
  panel.grid.major = element_blank(),
  axis.ticks = element_blank(),
  # legend.position = c(1.055, 0.5),
  legend.direction = "vertical",
  legend.title = element_text(size = 12, face = "bold")
)

migrate

baSumm$mean = as.numeric(baSumm$mean)
baSumm$hpdLow = as.numeric(baSumm$hpdLow)
baSumm$hpdHigh = as.numeric(baSumm$hpdHigh)

baSummSelf = baSumm %>% filter(pop.i == pop.j) %>% mutate(popdepth = paste(site.i, depth.i)) %>% mutate(retention = mean) %>% dplyr::select(-mean)

fknmsPopsMigrate2 = fknmsSites %>% group_by(site, depthZone, siteID) %>% dplyr::summarise(latDD = first(latDD), longDD = first(longDD)) %>% dplyr::filter(siteID %in% c("Ian's Lumps Site 52", "Site 48", "Site 47", "Site 45", "Site 35/36", "Site 37", "Site 39", "Site 19")) %>% dplyr::select(-site) %>% droplevels() %>% mutate(popdepth = paste(site, depthZone)) %>% as.data.frame() %>% slice(-5) %>% left_join(dplyr::select(baSummSelf, popdepth, retention))
## `summarise()` has grouped output by 'site', 'depthZone'. You can override using the
## `.groups` argument.
## Adding missing grouping variables: `site`
## Joining with `by = join_by(popdepth)`
fknmsPopsMigrate = fknmsPopsMigrate2[c(1,2,4,3,5:8),]

migratePal = c("Upper Keys" = flPal[1], "Lower Keys" = flPal[2], "Tortugas Bank" = flPal[3], "Riley's Hump" = flPal[4])

lines = c("Shallow" = 5, "Mesophotic" = 1)

baMapData = dplyr::select(baSumm, -mean2) %>% left_join(dplyr::select(fknmsPopsMigrate,-retention,-popdepth), by = c("site.i" = "site", "depth.i" = "depthZone")) %>% left_join(dplyr::select(fknmsPopsMigrate,-retention,-popdepth),, by = c("site.j" = "site", "depth.j" = "depthZone"), suffix = c(".i", ".j")) %>% filter(mean >= 0.02)

for(x in 1:nrow(baMapData)) {
  if (baMapData$pop.i[x] == baMapData$pop.j[x]) {
    baMapData$latDD.i[x] = NA;
    baMapData$latDD.j[x] = NA;
    baMapData$longDD.i[x] = NA;
    baMapData$longDD.j[x] = NA;
    baMapData$mean[x] = NA;
    baMapData$median[x] = NA
  }
}

migrateMap = ggplot() +
  geom_sf(data = florida, fill = "white", size = 0.25) +

# SHALLOW SOURCES
geom_curve(data = baMapData[6,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.02, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -1.5) +

geom_curve(data = baMapData[8,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.2) +

geom_curve(data = baMapData[9,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.02, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.2) +

geom_curve(data = baMapData[12,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.03, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 1.1) +  

geom_curve(data = baMapData[14,], aes(x = longDD.j, y = latDD.j, xend = longDD.i, yend = latDD.i-0.02, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -4) + 

geom_curve(data = baMapData[16,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.1) + 

geom_curve(data = baMapData[17,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.2) + 

  
# MESO SOURCES  
geom_curve(data = baMapData[2,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.2) +     

geom_curve(data = baMapData[3,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.2) +       

geom_curve(data = baMapData[5,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -1) +    

geom_curve(data = baMapData[7,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.4) + 

geom_curve(data = baMapData[10,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.3) + 

geom_curve(data = baMapData[11,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 1.4) +  

geom_curve(data = baMapData[15,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.02, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.2) +  
  
geom_curve(data = baMapData[18,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.2) + 
  
geom_curve(data = baMapData[20,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.3) +    
  
geom_curve(data = baMapData[21,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.2) +

geom_curve(data = baMapData[24,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.2) +

geom_curve(data = baMapData[25,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -6) +

geom_curve(data = baMapData[27,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i-0.01, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.4) +

geom_curve(data = baMapData[28,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.2) +

geom_curve(data = baMapData[30,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.02, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 2) +

geom_curve(data = baMapData[31,], aes(x = longDD.j, y = latDD.j, xend = longDD.i-0.01, yend = latDD.i-0.01, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = 0.3) +
  
geom_curve(data = baMapData[23,], aes(x = longDD.j, y = latDD.j, xend = longDD.i+0.01, yend = latDD.i, color = site.j, linetype = depth.j, size = mean), alpha = 0.7, arrow = arrow(type = "open", length = unit(0.03, "npc")), curvature = -0.1) +

  scale_fill_manual(values = migratePal, name = "Source site") +
  scale_color_manual(values = migratePal, guide = NULL) +
  scale_shape_manual(values = c(21, 23), name = "Depth") +
  scale_size(range = c(0.5, 2), breaks = c(0.02,0.06,0.1,0.14,0.18,0.22,0.26),name = expression(paste("Migration (", italic("m"), ")", sep = "")), guide = guide_legend(ncol = 1, order = 5)) +
  geom_point(data = fknmsPopsMigrate, aes(x = longDD, y = latDD, fill = site, shape = depthZone), size = 3.5) +
  scale_linetype_manual(values = lines, name = "Source depth") +
  coord_sf(xlim = c(-83.1, -80.25), ylim = c(24.3, 25.3)) +
  scale_x_continuous(breaks = c(seq(-84, -80, by = .5))) +
  scale_y_continuous(breaks = c(seq(24, 26, by = .2))) +
  annotation_scale(location = "br") +
  annotation_north_arrow(location = "br", which_north = "true", style = north_arrow_minimal(), pad_x = unit(-0.25, "cm") , pad_y = unit(0.75, "cm")) +
  guides(fill = guide_legend(override.aes = list(shape = 22, color = NA, size = 4),ncol = 1, order = 1, reverse = TRUE), shape = guide_legend(override.aes = list(size = 3), order = 2)) +
  theme_bw() +
  theme(panel.background = element_rect(fill = "aliceblue"),
        panel.border = element_rect(color = "black", size = 0.75, fill = NA),
        plot.background = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_line(color = "black"),
        axis.text = element_text(color = "black"),
        plot.title = element_blank(),
        legend.key.size = unit(15, "pt"),
        legend.spacing = unit(-5, "pt"),
        legend.position = "right",
        legend.direction = "vertical",
        legend.box = "vertical",
        legend.background = element_blank()
  )

migrateMap

Putting the plots together into a single figure panel

migrationPlots = (migrate/(migrateMap) + theme(legend.box.margin = margin(-20, 0, 0, 0))) + plot_layout(heights = c(1, 1.1)) + plot_annotation(tag_levels = 'A') & theme(plot.tag = element_text(size = 20))

ggsave("../figures/figure5.png", plot = migrationPlots, width = 28, height = 24, units = "cm", dpi = 300)

ggsave("../figures/figure5.svg", plot = migrationPlots, width = 28, height = 25, units = "cm", dpi = 300)

S. intersepta algal symbiont community structure


Now let’s examine algal symbiont communities with the results of SymPortal analysis of Symbiodiniaceae ITS2 sequences.

ITS2 Data

How many raw reads?

rawItsReads = read.delim("../data/ITS2/sintItsReadCounts", header = FALSE)
colnames(rawItsReads) = c("sample", "reads")

rawItsReads$sample = gsub("_S.*", "", rawItsReads$sample)
rawItsReads = rawItsReads %>% group_by(sample) %>% summarise(reads = first(reads))

head(rawItsReads)
## # A tibble: 6 × 2
##   sample reads
##   <chr>  <int>
## 1 SFK001 34806
## 2 SFK002 38328
## 3 SFK003 18122
## 4 SFK004 52311
## 5 SFK005 30791
## 6 SFK006 40545
#total reads
sum(rawItsReads$reads)
## [1] 7385411
#average reads/sample
(sum(rawItsReads$reads)/nrow(rawItsReads))
## [1] 33723.34
its2Seqs = read.delim("../data/ITS2/148_20210301_DBV_20210401T112728.seqs.absolute.abund_CLEAN.txt", header = TRUE)
its2Profs = read.csv("../data/ITS2/148_20210301_DBV_20210401T112728.profiles.absolute.abund_CLEAN.csv", header = TRUE, check.names = FALSE) 

head(its2Seqs)
##   Sample Symbiodinium  A3 A3b A3at A3ax X43947_A X34778_A X495083_A X36534_A X34149_A
## 1 SFK115            0  12   0    0    0        0        0         0        0        0
## 2 SFK022            0   7   0    0    0        0        0         0        0        0
## 3 SFK025           28 242   7    7    0        6        0         0        7        0
## 4 SFK095            0  14   0    0    0        0        0         0        0        0
## 5 SFK170            0  26   0    0    0        0        0         0        0        0
## 6 SFK175            0   5   0    0    0        0        0         0        0        0
##   X50854_A A3av A3s A3q X33981_A X1402229_A A3au A3aw X50835_A X34175_A X33953_A A3r
## 1        0    0   0   0        0          0    0    0        0        0        0   0
## 2        0    0   0   0        0          0    0    0        0        0        0   0
## 3        0    0  17  23        5          0    0    0        0        0        0   6
## 4        0    0   0   0        0          0    0    0        0        0        0   0
## 5        0    0   0   0        0          0    0    0        0        0        0   0
## 6        0    0   0   0        0          0    0    0        0        0        0   0
##   X1402205_A X364481_A X363583_A A4 X1402230_A X50833_A X50842_A X363143_A X72388_A
## 1          0         0         0  0          0        0        0         0        0
## 2          0         0         0  0          0        0        0         0        0
## 3          0         0         0  0          0        0        0        10        0
## 4          0         0         0  0          0        0        0         0        0
## 5          0         0         0  0          0        0        0         0        0
## 6          0         0         0  7          0        0        0         0        0
##   X363606_A X797686_A X22386_A X529468_A X34696_A X363636_A X1402231_A X34151_A
## 1         0         0        0         0        0         0          0        0
## 2         0         0        0         0        0         0          0        0
## 3         0         0        0         0        0         0          0        0
## 4         0         0        0         0        0         0          0        0
## 5         0         0        0         0        0         0          0        0
## 6         0         0        0         0        0         0          0        0
##   X364459_A X363570_A X363645_A X363578_A X1402232_A X364267_A X363625_A X50845_A
## 1         0         0         0         0          0         0         0        0
## 2         0         0         0         0          0         0         0        0
## 3         0         0         0         0          0         0         0        0
## 4         0         0         0         0          0         0         0        0
## 5         0         0         0         0          0         0         0        0
## 6         0         0         0         0          0         0         0        0
##   X363142_A X1402267_A X22415_A X363639_A X905679_A X363598_A X363706_A X363685_A
## 1         0          0        0         0         0         0         0         0
## 2         0          0        0         0         0         0         0         0
## 3         0          0        0         0         0         0         0         0
## 4         0          0        0         0         0         0         0         0
## 5         0          0        0         0         0         0         0         0
## 6         0          0        0         0         0         0         0         0
##   X1402206_A X1402233_A X1402235_A X43753_A X500385_A X1402234_A A3d X1402202_A
## 1          0          0          0        0         0          0   0          0
## 2          0          0          0        0         0          0   0          0
## 3          0          0          0        0         0          0   0          0
## 4          0          0          0        0         0          0   0          0
## 5          0          0          0        0         0          0   0          0
## 6          0          0          0        0         0          0   0          0
##   X1402236_A X364620_A X363593_A X367833_A X22400_A X50850_A X22463_A X363687_A
## 1          0         0         0         0        0        0        0         0
## 2          0         0         0         0        0        0        0         0
## 3          0         0         0         0        0        0        0         0
## 4          0         0         0         0        0        0        0         0
## 5          0         0         0         0        0        0        0         0
## 6          0         0         0         0        0        0        0         0
##   X693524_A X37988_A X66961_A X22436_A X363590_A X22426_A X45527_A A6b X36953_A
## 1         0        0        0        0         0        0        0   0        0
## 2         0        0        0        0         0        0        0   0        0
## 3         0        0        0        0         0        0        0   0        0
## 4         0        0        0        0         0        0        0   0        0
## 5         0        0        0        0         0        0        0   0        0
## 6         0        0        0        0         0        0        0   0        0
##   X363563_A X37985_A X693526_A X22451_A X33927_A X1402203_A A4.3 X35200_A X22392_A
## 1         0        0         0        0        0          0    0        0        0
## 2         0        0         0        0        0          0    0        0        0
## 3         0        0         0        0        0          0    0        0        0
## 4         0        0         0        0        0          0    0        0        0
## 5         0        0         0        0        0          0    0        0        0
## 6         0        0         0        0        0          0    0        0        0
##   X65140_A X1402245_A X364567_A X364639_A X1402216_A X22464_A X22429_A X49905_A
## 1        0          0         0         0          0        0        0        0
## 2        0          0         0         0          0        0        0        0
## 3        0          0         0         0          0        0        0        0
## 4        0          0         0         0          0        0        0        0
## 5        0          0         0         0          0        0        0        0
## 6        0          0         0         0          0        0        0        0
##   X49571_A X29211_A X36825_A X364218_A X1402237_A X363617_A X73521_A X364172_A
## 1        0        0        0         0          0         0        0         0
## 2        0        0        0         0          0         0        0         0
## 3        0        0        0         0          0         0        0         0
## 4        0        0        0         0          0         0        0         0
## 5        0        0        0         0          0         0        0         0
## 6        0        0        0         0          0         0        0         0
##   X363654_A X1402274_A X49906_A X37990_A X50843_A X363674_A X363646_A X33878_A
## 1         0          0        0        0        0         0         0        0
## 2         0          0        0        0        0         0         0        0
## 3         0          0        0        0        0         0         0        0
## 4         0          0        0        0        0         0         0        0
## 5         0          0        0        0        0         0         0        0
## 6         0          0        0        0        0         0         0        0
##   X1402268_A X1402282_A X22444_A X373280_A X1402238_A X366219_A X69439_A Breviolum B5
## 1          0          0        0         0          0         0        0         0  0
## 2          0          0        0         0          0         0        0         0  0
## 3          0          0        0         0          0         0        0         0  0
## 4          0          0        0         0          0         0        0         0  0
## 5          0          0        0         0          0         0        0         0  0
## 6          0          0        0         0          0         0        0         0  0
##   B18c B18b X1402208_B X1402209_B X45548_B X1402210_B X43411_B X37534_B X1402211_B
## 1    0    0          0          0        0          0        0        0          0
## 2    0    0          0          0        0          0        0        0          0
## 3    0    0          0          0        0          0        0        0          0
## 4    0    0          0          0        0          0        0        0          0
## 5    0    0          0          0        0          0        0        0          0
## 6    0    0          0          0        0          0        0        0          0
##   X1402212_B X1402213_B X1160454_B X1402214_B X1402215_B X37591_B B5ai X1402254_B
## 1          0          0          0          0          0        0    0          0
## 2          0          0          0          0          0        0    0          0
## 3          0          0          0          0          0        0    0          0
## 4          0          0          0          0          0        0    0          0
## 5          0          0          0          0          0        0    0          0
## 6          0          0          0          0          0        0    0          0
##   X71511_B X1402256_B X71527_B X1402255_B X1402257_B X71517_B X71509_B X71508_B
## 1        0          0        0          0          0        0        0        0
## 2        0          0        0          0          0        0        0        0
## 3        0          0        0          0          0        0        0        0
## 4        0          0        0          0          0        0        0        0
## 5        0          0        0          0          0        0        0        0
## 6        0          0        0          0          0        0        0        0
##   X1402258_B X71518_B X71510_B X1402259_B X368876_B X1402260_B X50427_B X1402262_B
## 1          0        0        0          0         0          0        0          0
## 2          0        0        0          0         0          0        0          0
## 3          0        0        0          0         0          0        0          0
## 4          0        0        0          0         0          0        0          0
## 5          0        0        0          0         0          0        0          0
## 6          0        0        0          0         0          0        0          0
##   X71525_B X71523_B X71515_B X368004_B X1402261_B X71526_B X71524_B X1402266_B
## 1        0        0        0         0          0        0        0          0
## 2        0        0        0         0          0        0        0          0
## 3        0        0        0         0          0        0        0          0
## 4        0        0        0         0          0        0        0          0
## 5        0        0        0         0          0        0        0          0
## 6        0        0        0         0          0        0        0          0
##   X1402265_B X1402264_B X1402263_B X43545_B X1402252_B X900132_B X1390171_B X1402253_B
## 1          0          0          0        0          0         0          0          0
## 2          0          0          0        0          0         0          0          0
## 3          0          0          0        0          0         0          0          0
## 4          0          0          0        0          0         0          0          0
## 5          0          0          0        0          0         0          0          0
## 6          0          0          0        0          0         0          0          0
##   B1 X1402276_B X1402280_B X38112_B Cladocopium    C3 C1 C16 C3go C3.10 C42.2 C1dl C3gm
## 1  0          0          0        0         761 10037  0   0    0   123     0    0    0
## 2  0          0          0        0        1273 15896  0   0    0   260     0    0    0
## 3  0          0          0        0         661 11333  0   0    0   115     0    0    0
## 4  0          0          0        0        1019 13663  0   0    0   717     0    0    0
## 5  0          0          0        0        1077 12007  0   0    0   619     0    0    0
## 6  0          0          0        0        1975 21478  0   0    0   385     0    0  110
##   C3gl C3hb C3gr C16b X110271_C X334025_C C3gk C1dk X22330_C X11408_C X18596_C X21897_C
## 1    0    0    0    0         0         0    0    0       58        0        0       90
## 2    0    0    0    0       164       158    0    0      150        0        0      157
## 3    0    0    0    0        67        68    0    0       85        0        0       91
## 4    0    0    0    0         0         0    0    0        0        0        0      227
## 5    0    0    0    0       102       120    0    0       83        0        0       93
## 6    0    0    0    0       282       260    0    0      210        0        0      274
##   C6c C3gq C3gp X65808_C C3gn C15hx C3dw C1cy X1402187_C X20795_C C93.1 X65703_C
## 1   0    0    0       54    0     0    0    0        181        0     0      144
## 2   0    0    0        0    0     0    0    0        120        0     0        0
## 3   0    0    0        0    0     0    0    0          0        0     0       68
## 4   0    0    0       88    0     0    0    0        133        0   229       96
## 5   0    0    0      106    0     0    0    0        131        0    81      120
## 6   0    0    0      166    0     0    0    0        303        0     0        0
##   X385070_C C3ge X1402188_C X1372_C X3238_C X95094_C C3hc X24879_C X91373_C X3699_C
## 1         0    0        107       0       0        0    0        0        0       0
## 2         0    0         93       0     135        0    0      188        0     108
## 3         0    0         76       0       0        0    0      100        0       0
## 4         0    0        120     185       0        0    0        0        0       0
## 5         0    0         89      82       0        0    0        0        0       0
## 6         0    0        128       0       0        0    0      120        0       0
##   C3gt C3dz X20934_C C1af C3gs X25557_C X40208_C X470358_C X40209_C X1402193_C X2239_C
## 1    0    0        0    0    0        0        0         0        0          0       0
## 2    0    0        0    0    0        0        0         0        0          0       0
## 3    0    0        0    0    0        0        0         0        0          0       0
## 4    0    0        0    0    0      170        0         0        0          0       0
## 5    0    0        0    0    0        0        0         0        0          0       0
## 6    0    0      232    0    0        0        0         0        0          0       0
##   X1402195_C C16a X17495_C X17534_C X2097_C X40211_C X93722_C C1v X40207_C X40212_C
## 1          0    0        0        0       0        0        0   0        0        0
## 2          0    0        0        0       0        0        0   0        0        0
## 3         55    0        0        0       0        0        0   0        0        0
## 4          0    0        0        0       0        0        0   0        0        0
## 5          0    0        0        0       0        0        0   0        0        0
## 6          0    0        0        0       0        0        0   0        0        0
##   X1402196_C X1402197_C X9944_C X1402198_C X1402219_C X470998_C X54162_C X22574_C
## 1          0          0       0          0          0         0        0        0
## 2          0          0       0          0          0         0        0        0
## 3          0          0       0          0          0         0        0        0
## 4          0          0       0          0          0         0        0        0
## 5          0          0      94          0          0         0        0        0
## 6          0          0       0          0          0         0        0        0
##   X20921_C X33343_C X1402200_C X25492_C X1402218_C X3240_C X2037_C X85729_C X5371_C
## 1        0        0          0        0          0       0       0        0       0
## 2        0        0          0        0          0       0       0        0       0
## 3        0        0          0       51          0       0       0        0       0
## 4        0        0          0        0          0       0       0        0       0
## 5        0        0          0       57          0       0       0        0       0
## 6        0        0          0        0          0       0       0        0       0
##   X1402225_C X909389_C X1402207_C C3ag X2428_C X1402220_C X4062_C X103828_C X1402199_C
## 1          0         0          0    0       0          0       0       127          0
## 2          0         0          0    0       0          0       0         0          0
## 3          0         0          0    0       0          0       0         0          0
## 4          0         0          0    0       0          0       0         0          0
## 5          0         0          0    0       0          0       0         0          0
## 6          0         0          0    0       0          0       0         0          0
##   X1398518_C X90670_C X1402204_C X1402227_C X1402248_C C6b X1402247_C X1402194_C X870_C
## 1          0        0          0          0          0   0          0          0      0
## 2          0        0          0          0          0   0          0          0      0
## 3          0        0          0          0          0   0          0          0      0
## 4          0        0          0          0          0   0          0          0      0
## 5          0        0          0          0          0   0          0          0      0
## 6          0        0          0          0          0   0          0          0      0
##   X71029_C C3ga X91285_C X1402192_C X1402244_C C1bz X18746_C X1402228_C X694_C C3i
## 1        0    0        0          0          0    0        0          0      0   0
## 2        0    0        0          0          0    0        0          0      0   0
## 3        0    0        0          0          0    0        0          0      0   0
## 4        0    0        0          0          0    0        0          0      0   0
## 5        0    0        0          0          0    0        0          0      0   0
## 6        0    0        0          0          0    0        0          0      0   0
##   X1402250_C X1402243_C C21 X1402281_C C3ca X9108_C X11201_C X11191_C X7821_C
## 1          0          0   0          0    0       0        0        0       0
## 2          0          0   0          0    0       0        0        0       0
## 3          0          0   0          0    0       0        0        0       0
## 4          0          0   0          0    0       0        0        0       0
## 5          0          0   0          0    0       0        0        0       0
## 6          0          0   0          0    0       0        0        0       0
##   X1402273_C C3ck X1402240_C X1402249_C X99988_C X1356_C X69324_C X24193_C C3bb C40f
## 1          0    0          0          0        0       0        0        0    0    0
## 2          0    0          0          0        0       0        0        0    0    0
## 3          0    0          0          0        0       0        0        0    0    0
## 4          0    0          0          0        0       0        0        0    0    0
## 5          0    0          0          0        0       0        0        0    0    0
## 6          0    0          0          0        0       0        0        0    0    0
##   X1401572_C X47282_C X16815_C X5726_C X1402270_C X1402221_C C1ap X1402275_C X21673_C
## 1          0        0        0       0          0          0    0          0        0
## 2          0        0        0       0          0          0    0          0        0
## 3          0        0        0       0          0          0    0          0        0
## 4          0        0        0       0          0          0    0          0        0
## 5          0        0        0       0          0          0    0          0        0
## 6          0        0        0       0          0          0    0          0        0
##   X69758_C C1bt X1402269_C X2943_C C70 X1402271_C X42218_C X1402277_C X9807_C C1ai C3t
## 1        0    0          0       0   0          0        0          0       0    0   0
## 2        0    0          0       0   0          0        0          0       0    0   0
## 3        0    0          0       0   0          0        0          0       0    0   0
## 4        0    0          0       0   0          0        0          0       0    0   0
## 5        0    0          0       0   0          0        0          0       0    0   0
## 6        0    0          0       0   0          0        0          0       0    0   0
##   X54249_C X26258_C X1402278_C X1402272_C C1x X40218_C X21804_C X1402279_C C3de
## 1        0        0          0          0   0        0        0          0    0
## 2        0        0          0          0   0        0        0          0    0
## 3        0        0          0          0   0        0        0          0    0
## 4        0        0          0          0   0        0        0          0    0
## 5        0        0          0          0   0        0        0          0    0
## 6        0        0          0          0   0        0        0          0    0
##   X13929_C X23354_C X1402223_C X99010_C X983542_C X3366_C X1402226_C X1402222_C
## 1        0        0          0        0         0       0          0          0
## 2        0        0          0        0         0       0          0          0
## 3        0        0          0        0         0       0          0          0
## 4        0        0          0        0         0       0          0          0
## 5        0        0          0        0         0       0          0          0
## 6        0        0          0        0         0       0          0          0
##   X42529_C X2152_C X62532_C X4558_C X2427_C X1829_C C3fo X18793_C X11809_C X31248_C
## 1        0       0        0       0       0       0    0        0        0        0
## 2        0       0        0       0       0       0    0        0        0        0
## 3        0       0        0       0       0       0    0        0        0        0
## 4        0       0        0       0       0       0    0        0        0        0
## 5        0       0        0       0       0       0    0        0        0        0
## 6        0       0        0       0       0       0    0        0        0        0
##   X1402241_C X816_C X921460_C C3ao C3an C3cn X3241_C X103581_C X21093_C X1402224_C
## 1          0      0         0    0    0    0       0         0        0          0
## 2          0      0         0    0    0    0       0         0        0          0
## 3          0      0         0    0    0    0       0         0        0          0
## 4          0      0         0    0    0    0       0         0        0          0
## 5          0      0         0    0    0    0       0         0        0          0
## 6          0      0         0    0    0    0       0         0        0          0
##   X18813_C X1402201_C X22869_C X23865_C C6a C1j X1402239_C X3434_C X22178_C X17016_C
## 1        0          0        0        0   0   0          0       0        0        0
## 2        0          0        0        0   0   0          0       0        0        0
## 3        0          0        0        0   0   0          0       0        0        0
## 4        0          0        0        0   0   0          0       0        0        0
## 5        0          0        0        0   0   0          0       0        0        0
## 6        0          0        0        0   0   0          0       0        0        0
##   X1402242_C X42518_C X54160_C X873_C C50f X1402246_C X1390080_C X113247_C X99987_C
## 1          0        0        0      0    0          0          0         0        0
## 2          0        0        0      0    0          0          0         0        0
## 3          0        0        0      0    0          0          0         0        0
## 4          0        0        0      0    0          0          0         0        0
## 5          0        0        0      0    0          0          0         0        0
## 6          0        0        0      0    0          0          0         0        0
##   X3601_C X1866_C X1402251_C X2895_C X9153_C C3fn X866_C X864_C X18159_C X990_C
## 1       0       0          0       0       0    0      0      0        0      0
## 2       0       0          0       0       0    0      0      0        0      0
## 3       0       0          0       0       0    0      0      0        0      0
## 4       0       0          0       0       0    0      0      0        0      0
## 5       0       0          0       0       0    0      0      0        0      0
## 6       0       0          0       0       0    0      0      0        0      0
##   X1402189_C X21205_C C3fc X871_C X1402190_C X8117_C X55844_C X54218_C X51874_C
## 1          0        0    0      0          0       0        0        0        0
## 2          0        0    0      0          0       0        0        0        0
## 3          0        0    0      0          0       0        0        0        0
## 4          0        0    0      0          0       0        0        0        0
## 5          0        0    0      0          0       0        0        0        0
## 6          0        0    0      0          0       0        0        0        0
##   X874099_C X27927_C C65b X46049_C X37410_C X28411_C D1 G3l G3b
## 1         0        0    0        0        0        0  0   0   0
## 2         0        0    0        0        0        0  0   0   0
## 3         0        0    0        0        0        0  0   0   0
## 4         0        0    0        0        0        0  0   0   0
## 5         0        0    0        0        0        0  0   0   0
## 6         0        0    0        0        0        0  0   0   0
sum(its2Seqs[,c(2:ncol(its2Seqs))])
## [1] 3280586
sum(its2Profs[,c(2:ncol(its2Profs))])
## [1] 2728837
its2SeqsGen = its2Seqs %>% rowwise() %>%  summarise(sample = Sample, symbiodinium = sum(c_across(2:121)), breviolum = sum(c_across(122:176)), cladocopium = sum(c_across(177:405)), durusdinium = sum(c_across(406)), gerakladium = sum(c_across(406:407)))

round(sum(its2SeqsGen$symbiodinium)/sum(its2SeqsGen[,-1])*100, 2)
## [1] 19.67
round(sum(its2SeqsGen$breviolum)/sum(its2SeqsGen[,-1])*100, 2)
## [1] 0.29
round(sum(its2SeqsGen$cladocopium)/sum(its2SeqsGen[,-1])*100, 2)
## [1] 80.03
round(sum(its2SeqsGen$durusdinium)/sum(its2SeqsGen[,-1])*100, 4)
## [1] 0.0002
round(sum(its2SeqsGen$gerakladium)/sum(its2SeqsGen[,-1])*100, 4) 
## [1] 0.002
its2ProfsGen = its2Profs %>% rowwise() %>%  summarise(sample = Sample, symbiodinium = sum(c_across(2:7)), breviolum = sum(c_across(8:10)), cladocopium = sum(c_across(11:20)))

round(sum(its2ProfsGen$symbiodinium)/sum(its2ProfsGen[,-1])*100, 2)
## [1] 21.33
round(sum(its2ProfsGen$breviolum)/sum(its2ProfsGen[,-1])*100, 2)
## [1] 0.22
round(sum(its2ProfsGen$cladocopium)/sum(its2ProfsGen[,-1])*100, 2)
## [1] 78.45

Read in SymPortal outputs for ITS2 type profiles

stephanocoeniaMetaData = read.csv("../data/stephanocoeniaMetaData.csv", header = TRUE, check.names = FALSE)[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select(c(sample = tubeID, site, depthM, depthZone))

its2Profs = read.csv("../data/ITS2/148_20210301_DBV_20210401T112728.profiles.absolute.abund_CLEAN.csv", header = TRUE, check.names = FALSE) %>% rename(sample = Sample)

its2Profs = stephanocoeniaMetaData %>% right_join(its2Profs) %>% arrange(sample) 
## Joining with `by = join_by(sample)`
its2Profs$site = factor(its2Profs$site)
its2Profs$site = factor(its2Profs$site, levels(its2Profs$site)[c( 2, 3, 1, 4)])
its2Profs$depthZone = factor(its2Profs$depthZone)
its2Profs$depthZone = factor(its2Profs$depthZone, levels(its2Profs$depthZone)[c(2, 1)])

its2Profs = its2Profs %>% arrange(site, depthZone,   desc(`C3/C3.10`),  desc(`C1/C3-C42.2-C1dl-C3gl-C3gm-C3gk`), desc(`C3-C1-C3.10`), desc(`C3-C1dk-C15hx`), desc(`C3-C3go-C6c-C3gq-C3gp-C3gn-C3dw`), desc(`C16/C3-C16b`), desc(`C3-C3hb-C3ge-C3hc-C1dk`), desc(`C3-C3gr-C3gt-C3gs-C3.10`), desc(`C3/C1`),desc(`A3-A3b-A3at-A3ax`), desc(`A3-A3at-A3b-A3q-A3s`), desc(`A3-A3s-A3q`), desc(`A3`), desc(`A3-A3b-A3av-A3au-A3aw`),desc(`A4`), desc(`C3`), desc(`B18b`), desc(`B18c`), desc(`B5`))

sampleCounts = plyr::count(its2Profs, c('site','depthZone'))
meltedList = reshape2::melt(lapply(sampleCounts$freq,function(x){c(1:x)}))
its2Profs$barPlotOrder = meltedList$value
its2Profs = its2Profs[c(1,ncol(its2Profs),2:(ncol(its2Profs)-1))]

head(its2Profs)



ITS2 type profiles

Preparing ITS2 type profiles for plotting

its2ProfsPerc = its2Profs
its2ProfsPerc$sum = apply(its2ProfsPerc[, c(6:length(its2ProfsPerc[1,]))], 1, function(x) {
sum(x, na.rm = T)
})

its2ProfsPerc = cbind(its2ProfsPerc[, c(1:5)], (its2ProfsPerc[, c(6:(ncol(its2ProfsPerc)-1))]
/ its2ProfsPerc$sum))
head(its2ProfsPerc)
##   sample barPlotOrder         site depthM depthZone A3-A3b-A3at-A3ax
## 1 SFK068            1 Riley's Hump   27.4   Shallow                0
## 2 SFK091            2 Riley's Hump   26.2   Shallow                0
## 3 SFK073            3 Riley's Hump   26.2   Shallow                0
## 4 SFK084            4 Riley's Hump   26.2   Shallow                0
## 5 SFK083            5 Riley's Hump   26.2   Shallow                0
## 6 SFK082            6 Riley's Hump   26.5   Shallow                0
##   A3-A3at-A3b-A3q-A3s A3-A3s-A3q A3 A3-A3b-A3av-A3au-A3aw A4 B18b B18c B5 C3/C3.10
## 1                   0          0  0                     0  0    0    0  0        0
## 2                   0          0  0                     0  0    0    0  0        0
## 3                   0          0  0                     0  0    0    0  0        0
## 4                   0          0  0                     0  0    0    0  0        0
## 5                   0          0  0                     0  0    0    0  0        0
## 6                   0          0  0                     0  0    0    0  0        0
##   C1/C3-C42.2-C1dl-C3gl-C3gm-C3gk C3-C1-C3.10 C3-C1dk-C15hx
## 1                               1           0             0
## 2                               1           0             0
## 3                               1           0             0
## 4                               1           0             0
## 5                               1           0             0
## 6                               1           0             0
##   C3-C3go-C6c-C3gq-C3gp-C3gn-C3dw C16/C3-C16b C3-C3hb-C3ge-C3hc-C1dk
## 1                               0           0                      0
## 2                               0           0                      0
## 3                               0           0                      0
## 4                               0           0                      0
## 5                               0           0                      0
## 6                               0           0                      0
##   C3-C3gr-C3gt-C3gs-C3.10 C3/C1 C3
## 1                       0     0  0
## 2                       0     0  0
## 3                       0     0  0
## 4                       0     0  0
## 5                       0     0  0
## 6                       0     0  0
# check that all proportions add up to 1
apply(its2ProfsPerc[, c(6:(ncol(its2ProfsPerc)))], 1, function(x) {
sum(x, na.rm = T)
})
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [42] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [83] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [124] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [165] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [206] 1 1 1 1 1 1 1 1 1 1 1 1 1 1
admixOrd = fkSintAdmix %>% dplyr::select(sample, ord) 
pcangsdITS = pcangsd %>% dplyr::select(sample, cluster)

its2ProfsPerc = its2ProfsPerc %>% left_join(admixOrd) %>% relocate(ord,.after = barPlotOrder) %>% left_join(pcangsdITS) %>% relocate(cluster, .after = depthZone)
## Joining with `by = join_by(sample)`
## Joining with `by = join_by(sample)`

Everything looks good and is ready to plot

gssProf = otuStack(its2ProfsPerc, count.columns = c(8:length(its2ProfsPerc[1, ])),
 condition.columns = c(1:7)) %>% filter(otu != "summ") %>% droplevels() # remove summ rows

levels(gssProf$otu)
levels(gssProf$depthZone)
levels(gssProf$site)



Consruct ITS2 type profile barplot

zooxAnno = data.frame(x1 = c(0.5, 0.5, 0.5, 0.5), x2 = c(30.5, 30.5, 30.5, 30.5),
                     y1 = -0.22, y2 = -0.22, site = c("Riley's Hump", "Tortugas Bank", "Lower Keys", "Upper Keys"))

zooxAnno$site = factor(zooxAnno$site)
zooxAnno$site = factor(zooxAnno$site, levels = levels(zooxAnno$site)[c(2, 3, 1, 4)])


gssProfPlot = gssProf %>% left_join(zooxAnno, by = "site")
gssProfPlot$ord = as.numeric(gssProfPlot$ord)
  
its2ProfsPlotA = ggplot(gssProfPlot, aes(x = ord, y = count, fill = otu)) +
  geom_bar(position = "stack", stat = "identity", color = "gray25", size = 0.25) + 
  scale_fill_manual(values = profPal, name = expression(paste(italic("ITS2"), " type profile"))) +
 
  geom_segment(data = gssProfPlot, aes(x = ord-0.5, xend = ord+0.5, color = cluster), y = -.07, yend = -.07, linewidth = 4) +
  scale_color_manual(values = kColPal, guide = "none") +
  ggnewscale::new_scale_color() +
  
  geom_segment(data = gssProfPlot %>% filter(depthZone == "Mesophotic"), aes(x = x1, xend = x2, y = y1, yend = y2, color = site), linewidth = 7) +
  scale_color_manual(values = rev(flPal)) +
  
   geom_text(data = (gssProfPlot %>% filter(depthZone == "Mesophotic", site %in% c("Riley's Hump", "Tortugas Bank"), sample %in% c("SFK001", "SFK100"), otu == "A4")), x = 15.5, y = -.205, aes(label = site), size = 4, color = "#FFFFFF") +
  geom_text(data = (gssProfPlot %>% filter(depthZone == "Mesophotic", site %in% c("Lower Keys", "Upper Keys"), sample %in% c("SFK101", "SFK201"), otu == "A4")), x = 15.5, y = -.205, aes(label = site), size = 4, color = "#000000") +
  labs(title = expression(italic("SymPortal")), fill = expression(paste(italic("ITS2"), " type profile"))) +
  
  guides(color = "none", fill = guide_legend(ncol = 3, reverse = FALSE)) +
  facet_grid(factor(depthZone) ~ site, scales = "free", switch = "both", space = "free") + # faceting plots by Depth and Site
  
  coord_cartesian(ylim = c(0, 1), xlim = c(0.5, 30.5), clip = "off") +
  scale_x_discrete(expand = c(0.005, 0.005)) +
  scale_y_continuous(expand = c(0.001, 0.001)) +
theme_bw()

its2ProfsPlot = its2ProfsPlotA +
theme(plot.title = element_text(),
  panel.grid = element_blank(),
  # panel.background = element_blank(),
  panel.background = element_rect(fill = "gray70"),
  panel.border = element_rect(fill = NA, color = "black", size = 0.75, linetype = "solid"),
  plot.background = element_blank(),
  legend.background = element_blank(),
  panel.spacing.x = grid:::unit(0.05, "lines"),
  panel.spacing.y = grid:::unit(0.78, "lines"),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks.x = element_blank(),
  axis.ticks.y = element_blank(),
  axis.title = element_blank(),
  strip.background.x = element_blank(),
  strip.background.y = element_blank(),
  strip.text = element_text(size = 12),
  strip.text.y.left = element_text(angle = 90),
  strip.text.x.bottom = element_text(vjust = .75, color = NA),
  legend.key.size = unit(0.75, "line"),
  legend.key = element_blank(),
  legend.title = element_text(size = 10, angle = 90),
  legend.text = element_text(size = 8),
  legend.position = "right")

# its2ProfsPlot



Structure plots

structurePlots = ((pcaPlot12S + theme(axis.title.y = element_text(margin = ggplot2::margin(r = -20, unit = "pt")))) | pcaPlot12L | pcaPlot23L)/((admixPlot + labs(title = expression(paste(italic("S. intersepta"))))) | (its2ProfsPlot + guides(color = "none", fill = guide_legend(ncol = 1, reverse = FALSE)) + labs(title = "Symbiodiniaceae", fill = expression(paste(italic("ITS2"), " type profile"))) + theme(legend.title = element_text(size = 8, angle = 0), plot.title = element_text(size = 10), legend.text = element_text(size = 6), strip.text.y.left = element_text(angle = 90, size = 10),strip.text.x = element_text(size = 8)))) +
plot_annotation(tag_levels = 'A') &
  theme(plot.tag = element_text(size = 16),
        plot.title = element_text(size = 10),
        plot.title.position =  "panel",
        legend.spacing = unit(-5, "pt"),
        legend.key = element_blank(),
        legend.background = element_blank())



ggsave("../figures/figure2.png", plot = structurePlots, height = 6.6, width = 12, units = "in", dpi = 300)

ggsave("../figures/figure2.svg", plot = structurePlots, height = 6.5, width = 10, units = "in", dpi = 300)

SNP vs ITS2 genera

Pulling genera of Symbiodiniaceae from SNPS and comparing to genera of ITS2 profiles from SymPortal

popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "site" = site, "depth" = depthZone)

zoox = read.delim("../data/snps/symbionts/zooxReads", header = FALSE, check.names = FALSE)

head(zoox)
##                               V1 V2
## 1 fk_S001.trim.zoox.zoox.bt2.bam NA
## 2                           chr1 77
## 3                           chr2 78
## 4                           chr3 87
## 5                           chr4 80
## 6                           chr5  2
# Reconstruct read mapping output into dataframe usable for analysis
zoox$V2[is.na(zoox$V2)] <- as.character(zoox$V1[is.na(zoox$V2)])
zoox$V1 = gsub(pattern = "fk_*", "chr", zoox$V1)
zoox$V2 = gsub(".trim.*", "", zoox$V2)
zoox = zoox %>% filter(zoox$V1 != "*")
zooxLst = split(zoox$V2, as.integer(gl(length(zoox$V2), 20, length(zoox$V2))))

zooxMaps = NULL

for(i in zooxLst){
  zooxMaps = rbind(zooxMaps, data.frame(t(i)))
}

# remove tech reps
zooxMaps = zooxMaps[-c(66, 68, 164, 166, 209, 211),]

# rename columns and samples to match other ITS2 dataframe
zooxMaps$X1 = gsub("fk_S", "SFK", zooxMaps$X1)
zooxMaps$X1 = gsub("\\.[1-3]", "", zooxMaps$X1)
colnames(zooxMaps) = c("sample",zoox$V1[c(2:20)])

# convert characters to numeric
str(zooxMaps)
## 'data.frame':    220 obs. of  20 variables:
##  $ sample: chr  "SFK001" "SFK002" "SFK003" "SFK004" ...
##  $ chr1  : chr  "77" "59" "37" "953" ...
##  $ chr2  : chr  "78" "82" "22" "1069" ...
##  $ chr3  : chr  "87" "79" "30" "1383" ...
##  $ chr4  : chr  "80" "118" "19" "1360" ...
##  $ chr5  : chr  "2" "2" "0" "18" ...
##  $ chr6  : chr  "24" "17" "40" "18" ...
##  $ chr7  : chr  "42" "67" "35" "13" ...
##  $ chr8  : chr  "63" "85" "57" "29" ...
##  $ chr9  : chr  "61" "58" "51" "30" ...
##  $ chr10 : chr  "3065" "3920" "3611" "2820" ...
##  $ chr11 : chr  "4388" "5206" "5077" "3605" ...
##  $ chr12 : chr  "4606" "5437" "5442" "3855" ...
##  $ chr13 : chr  "4294" "4919" "4995" "3485" ...
##  $ chr14 : chr  "3538" "4217" "4013" "3072" ...
##  $ chr15 : chr  "518" "566" "533" "437" ...
##  $ chr16 : chr  "29" "105" "13" "82" ...
##  $ chr17 : chr  "19" "59" "17" "28" ...
##  $ chr18 : chr  "13" "44" "4" "26" ...
##  $ chr19 : chr  "3" "6" "1" "5" ...
for(i in c(2:20)){
  zooxMaps[,i] = as.numeric(zooxMaps[,i])
  }

str(zooxMaps)
## 'data.frame':    220 obs. of  20 variables:
##  $ sample: chr  "SFK001" "SFK002" "SFK003" "SFK004" ...
##  $ chr1  : num  77 59 37 953 44 ...
##  $ chr2  : num  78 82 22 1069 68 ...
##  $ chr3  : num  87 79 30 1383 80 ...
##  $ chr4  : num  80 118 19 1360 53 ...
##  $ chr5  : num  2 2 0 18 4 105 0 0 0 0 ...
##  $ chr6  : num  24 17 40 18 9 34 19 26 43 20 ...
##  $ chr7  : num  42 67 35 13 22 35 21 57 71 50 ...
##  $ chr8  : num  63 85 57 29 32 33 40 60 80 86 ...
##  $ chr9  : num  61 58 51 30 39 32 30 84 72 43 ...
##  $ chr10 : num  3065 3920 3611 2820 1501 ...
##  $ chr11 : num  4388 5206 5077 3605 1807 ...
##  $ chr12 : num  4606 5437 5442 3855 2061 ...
##  $ chr13 : num  4294 4919 4995 3485 1736 ...
##  $ chr14 : num  3538 4217 4013 3072 1564 ...
##  $ chr15 : num  518 566 533 437 250 190 473 769 559 821 ...
##  $ chr16 : num  29 105 13 82 71 93 20 11 59 19 ...
##  $ chr17 : num  19 59 17 28 24 51 22 40 40 38 ...
##  $ chr18 : num  13 44 4 26 40 55 16 16 33 13 ...
##  $ chr19 : num  3 6 1 5 2 13 3 0 0 7 ...
# collapse fake chromosomes into representative genera
zooxMaps$Symbiodinium = rowSums(zooxMaps[2:6])
zooxMaps$Breviolum = rowSums(zooxMaps[7:10])
zooxMaps$Cladocopium = rowSums(zooxMaps[11:16])
zooxMaps$Durusdinium = rowSums(zooxMaps[17:20])

# keep genera totals and turn into proportions for barplot
zooxMaps = zooxMaps[,c(1, 21:24)]
zooxProp = zooxMaps
zooxProp$sum = apply(zooxProp[, c(2:length(zooxProp[1,]))], 1, function(x) {
sum(x, na.rm = T)
})
zooxProp = cbind(zooxProp$sample, (zooxProp[, c(2:(ncol(zooxProp)-1))]
/ zooxProp$sum))

colnames(zooxProp)[1] = "sample"

head(zooxProp)
##   sample Symbiodinium   Breviolum Cladocopium Durusdinium
## 1 SFK001  0.015438128 0.009053223   0.9724591 0.003049507
## 2 SFK002  0.013575022 0.009063323   0.9688174 0.008544279
## 3 SFK003  0.004500563 0.007625953   0.9864150 0.001458516
## 4 SFK004  0.214599785 0.004038047   0.7750359 0.006326274
## 5 SFK005  0.026469650 0.010842989   0.9481237 0.014563623
## 6 SFK006  0.718347383 0.005328456   0.2678941 0.008430094
# Check that all samples total to 1
apply(zooxProp[, c(2:(ncol(zooxProp)))], 1, function(x) {
sum(x, na.rm = T)
})
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
##  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
##  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  67 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
##  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
##  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 157 158 159 160 161 162 163 165 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 203 204 205 206 207 208 210 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
# add sample metadata to proportions
snpSym = popData %>% left_join(zooxProp)
## Joining with `by = join_by(sample)`

Combining SNP and ITS2 data for comparison of Symbiodiniaceae genera This will allow us to plot individuals in the same order across methods

#sum profiles into genera
symGenera = its2Profs
symGenera$itsSymbiodinium = rowSums(symGenera[6:11])
symGenera$itsBreviolum = rowSums(symGenera[12:14])
symGenera$itsCladocopium = rowSums(symGenera[15:24])
symGenera$itsDurusdinium = 0  

symGenera = symGenera %>% dplyr::select(sample, barPlotOrder, itsSymbiodinium, itsBreviolum, itsCladocopium, itsDurusdinium) %>% left_join(admixOrd) %>% relocate(ord, .after = barPlotOrder)
## Joining with `by = join_by(sample)`
#convert to proportions
symGenera$sum = apply(symGenera[, c(4:length(symGenera[1,]))], 1, function(x) {
sum(x, na.rm = T)
})

symGeneraProp = cbind(symGenera$sample, symGenera[, c(4:(ncol(symGenera)-1))]
/ symGenera$sum)

colnames(symGeneraProp)[1] = "sample"

#Check that all samples total to 1
apply(symGeneraProp[,c(2:5)], 1, function(x) {
sum(x, na.rm = T)
})
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [42] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [83] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [124] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [165] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [206] 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#construct combined dataframe
symGenera = symGenera %>% dplyr::select(sample, ord) %>% left_join(snpSym) %>%  left_join(symGeneraProp) 
## Joining with `by = join_by(sample)`
## Joining with `by = join_by(sample)`
symGenera$depth = factor(symGenera$depth)
symGenera$depth = factor(symGenera$depth, levels = levels(symGenera$depth)[c(2, 1)])

symGenera$site = factor(symGenera$site)
symGenera$site = factor(symGenera$site, levels = levels(symGenera$site)[c(2, 3, 1, 4)])

#turn into melted dataframe with otustack() and remove "summ" rows
gssSym = otuStack(symGenera, count.columns = c(5:length(symGenera[1, ])),
 condition.columns = c(1:4)) %>% filter(otu != "summ") %>% droplevels()

#check that levels are correct/ordered
levels(gssSym$otu)
## [1] "Symbiodinium"    "Breviolum"       "Cladocopium"     "Durusdinium"    
## [5] "itsSymbiodinium" "itsBreviolum"    "itsCladocopium"  "itsDurusdinium"
levels(gssSym$depth)
## [1] "Shallow"    "Mesophotic"
levels(gssSym$site)
## [1] "Riley's Hump"  "Tortugas Bank" "Lower Keys"    "Upper Keys"


Creating Symbiodiniaceae genera relative proportion barplots

SNPs:

gssSymPlot = gssSym %>% left_join(zooxAnno, by = "site") %>% left_join(pcangsdITS)
## Joining with `by = join_by(sample)`
gssSymPlot$ord = as.numeric(gssSymPlot$ord)

zooxSNPA = ggplot(data = subset(gssSymPlot, subset = otu %in% c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium" )), aes(x = ord, y = count, fill = otu, order = ord)) +
  geom_point(aes(x=1, y=0.5, fill = otu), shape = 22, size = 0) +
  geom_bar(stat = "identity", position = "stack", colour = "grey25", width = 1, size = 0.2, show.legend = FALSE) +
  xlab("Population") +
  
  scale_fill_manual(values = colPalZoox, name = "Symbiodiniaceae genus") +
   geom_segment(data = (subset(gssSymPlot, subset = otu %in% c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium" )) %>% filter(depth == "Mesophotic")), aes(x = x1, xend = x2, y = y1, yend = y2, color = site), size = 7) +
  scale_color_manual(values = rev(flPal), guide = "none") +
  ggnewscale::new_scale_color() +
  
  geom_segment(data = (subset(gssSymPlot, subset = otu %in% c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium" ))), aes(x = ord-0.5, xend = ord+0.5, color = cluster), y = -.07, yend = -.07, linewidth = 4) +
  scale_color_manual(values = kColPal, name = "Lineage") +
  
  coord_cartesian(ylim = c(0, 1), xlim = c(0.5, 30.5), clip = "off") +

  scale_x_discrete(expand = c(0.005, 0.005)) +
  scale_y_continuous(expand = c(0.001, 0.001)) +
  facet_grid(factor(depth) ~ site, drop = TRUE, scales = "free", switch = "both", space = "free") +

    geom_text(data = subset(gssSymPlot, subset = otu == "Symbiodinium") %>% filter(sample %in% c("SFK095", "SFK015")), x = 15.5, y = -.205, aes(label = site), size = 4.4, color = "#FFFFFF") +
  geom_text(data = subset(gssSymPlot, subset = otu == "Symbiodinium") %>% filter(sample %in% c("SFK195", "SFK156")), x = 15.5, y = -.205, aes(label = site), size = 4.4, color = "#000000") +

  ggtitle("2bRAD") +
  guides(color = guide_legend(override.aes = list(size = 4), ncol = 1), fill = "none")  +
  theme_bw()

zooxSNP = zooxSNPA + theme(plot.title = element_text(),
  panel.grid = element_blank(),
  # panel.background = element_blank(),
  panel.background = element_rect(fill = "gray70"),
  panel.border = element_rect(fill = NA, color = "black", size = 0.75, linetype = "solid"),
  panel.spacing.x = grid:::unit(0.05, "lines"),
  panel.spacing.y = grid:::unit(0.82, "lines"),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks.x = element_blank(),
  axis.ticks.y = element_blank(),
  axis.title = element_blank(),
  strip.background.x = element_blank(),
  strip.background.y = element_blank(),
  strip.text = element_text(size = 12),
  strip.text.y.left = element_text(size = 12, angle = 90),
  strip.text.x.bottom = element_text(vjust = -.1, color = NA),
  legend.title = element_text(size = 10),
  legend.text = element_text(size = 8),
  legend.key.size = unit(0.75, "line"),
  legend.key = element_blank(),
  legend.position = "bottom",
  legend.direction = "vertical",
  legend.box = "horizontal")

# zooxSNP


ITS2:

zooxITSA = ggplot(data = subset(gssSymPlot, subset = !(otu %in% c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium" ))), aes(x = ord, y = count, fill = otu, order = ord)) +
  geom_point(aes(x=1, y=0.5, fill = otu), shape = 22, size = 0) +
  geom_bar(stat = "identity", position = "stack", colour = "grey25", width = 1, size = 0.2, show.legend = FALSE) +
  xlab("Population") +
  scale_fill_manual(values = colPalZoox, name = "Symbiodiniaceae genus", labels = c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium")) +

    geom_segment(data = (subset(gssSymPlot, subset = !otu %in% c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium" ))), aes(x = ord-0.5, xend = ord+0.5, color = cluster), y = -.07, yend = -.07, linewidth = 4) +
  scale_color_manual(values = kColPal, guide = "none") +
  ggnewscale::new_scale_color() +
    
  geom_segment(data = (subset(gssSymPlot, subset = !otu %in% c("Symbiodinium", "Breviolum", "Cladocopium", "Durusdinium" )) %>% filter(depth == "Mesophotic")), aes(x = x1, xend = x2, y = y1, yend = y2, color = site), size = 7) +
  scale_color_manual(values = rev(flPal)) +
  
  coord_cartesian(ylim = c(0, 1), xlim = c(0.5, 30.5), clip = "off") +

  scale_x_discrete(expand = c(0.005, 0.005)) +
  scale_y_continuous(expand = c(0.001, 0.001)) +
  facet_grid(factor(depth) ~ site, drop = TRUE, scales = "free", switch = "both", space = "free") +

  geom_text(data = subset(gssSymPlot, subset = otu == "itsSymbiodinium") %>% filter(sample %in% c("SFK095", "SFK015")), x = 15.5, y = -.205, aes(label = site), size = 4.4, color = "#FFFFFF") +
  geom_text(data = subset(gssSymPlot, subset = otu == "itsSymbiodinium") %>% filter(sample %in% c("SFK195", "SFK156")), x = 15.5, y = -.205, aes(label = site), size = 4.4, color = "#000000") +
  
  guides(fill = guide_legend(override.aes = list(size = 4), ncol = 1), color = "none")  +
  labs(title = expression(italic("ITS2"))) +
  theme_bw()

zooxITS = zooxITSA + theme(plot.title = element_text(),
  panel.grid = element_blank(),
  # panel.background = element_blank(),
  panel.background = element_rect(fill = "gray70"),
  panel.border = element_rect(fill = NA, color = "black", size = 0.75, linetype = "solid"),
  panel.spacing.x = grid:::unit(0.05, "lines"),
  panel.spacing.y = grid:::unit(0.82, "lines"),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks.x = element_blank(),
  axis.ticks.y = element_blank(),
  axis.title = element_blank(),
  strip.background.x = element_blank(),
  strip.background.y = element_blank(),
  strip.text = element_text(size = 12),
  strip.text.y.left = element_text(size = 12, angle = 90),
  strip.text.x.bottom = element_text(vjust = -.1, color = NA),
  legend.title = element_text(size = 10),
  legend.text = element_text(size = 8, face = "italic"),
  legend.key = element_blank(),
  legend.key.size = unit(0.5, "line"),
  legend.position = "bottom",
  legend.direction = "vertical",
  legend.box = "horizontal")
 
# zooxITS

Symbiodiniaceae barplots

# its2Plots = (its2ProfsPlot + theme(legend.position = "right",  legend.title = element_text(angle = 0)) & guides(color = "none", fill = guide_legend(ncol = 1, reverse = FALSE)))/zooxITS/zooxSNP +

its2Plots = zooxITS/zooxSNP +
  plot_annotation(tag_levels = "A") &
  theme(plot.tag = element_text(size = 16), 
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "right",
        legend.title = element_text(color = "black", size = 10),
        legend.text = element_text(color = "black", size = 8))

ggsave("../figures/figure6.png", plot = its2Plots, height = 18, width = 20, units = "cm", dpi = 300)

ggsave("../figures/figure6.svg", plot = its2Plots, height = 18, width = 20, units = "cm", dpi = 300)

Procrustes analysis

Comparing the two outputs with procrustes analysis

popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "site" = site, "depth" = depthZone)

symSnpDf = zooxMaps %>% left_join(popData) %>% relocate(c(site, depth), .after = sample) %>% filter(!row_number()==131) %>% mutate(dataSet = "SNPs") %>% relocate(dataSet, .after = sample)
## Joining with `by = join_by(sample)`
rownames(symSnpDf) = symSnpDf$sample

symITS2 = its2Profs
symITS2$Symbiodinium = rowSums(symITS2[6:11])
symITS2$Breviolum = rowSums(symITS2[12:14])
symITS2$Cladocopium = rowSums(symITS2[15:24])
symITS2$Durusdinium = 0  

symITS2Df = symITS2 %>% dplyr::select(sample, Symbiodinium, Breviolum, Cladocopium, Durusdinium) %>% left_join(popData) %>% relocate(c(site, depth), .after = sample) %>% arrange(sample) %>% mutate(dataSet = "ITS2") %>% relocate(dataSet, .after = sample)
## Joining with `by = join_by(sample)`
rownames(symITS2Df) = symITS2Df$sample

#create distance matrices
symSnpdist = vegdist(decostand(symSnpDf[c(5:ncol(symSnpDf))], method = "normalize"), method = "bray")
    
symITS2dist = vegdist(decostand(symITS2Df[c(5:ncol(symITS2Df))], method = "normalize"), method = "bray")

snpPcOA = cmdscale(symSnpdist, eig = TRUE, x.ret = TRUE)
its2PcOA = cmdscale(symITS2dist, eig = TRUE, x.ret = TRUE)

#procrustes analysis
its2GeneraProcrustes = protest(Y = its2PcOA, X = snpPcOA, choices = c(1, 2), 
permutations = 9999, symmetric = FALSE)

its2GeneraProcrustes
## 
## Call:
## protest(X = snpPcOA, Y = its2PcOA, permutations = 9999, choices = c(1,      2), symmetric = FALSE) 
## 
## Procrustes Sum of Squares (m12 squared):        0.1561 
## Correlation in a symmetric Procrustes rotation: 0.9187 
## Significance:  0.0001 
## 
## Permutation: free
## Number of permutations: 9999
plot(its2GeneraProcrustes, kind = 1)

plot(its2GeneraProcrustes, kind = 2)

popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "site" = site, "depth" = depthZone)

symGenProcPlot = procrustes(X = snpPcOA, Y = its2PcOA, choices = c(1, 2), symmetric = FALSE)

symGenProcPlotData = cbind(symGenProcPlot$X, symGenProcPlot$Yrot) %>% as.data.frame()
rownames(symGenProcPlotData) = rownames(symGenProcPlot$X)
colnames(symGenProcPlotData) = c("x1", "y1", "x2", "y2")
symGenProcPlotData$sample = row.names(symGenProcPlotData)
symGenProcPlotData$sample = gsub(pattern = "\\.2", "", symGenProcPlotData$sample)
symGenProcPlotData = symGenProcPlotData %>% left_join(popData) %>% relocate(sample, .before = x1)
## Joining with `by = join_by(sample)`
symGenProcPlotData$depth = factor(symGenProcPlotData$depth)
symGenProcPlotData$depth = factor(symGenProcPlotData$depth, levels(symGenProcPlotData$depth)[c(2,1)])
symGenProcPlotData$site = factor(symGenProcPlotData$site)
symGenProcPlotData$site = factor(symGenProcPlotData$site, levels(symGenProcPlotData$site)[c(4, 1, 3, 2)])
  
head(symGenProcPlotData)
##   sample          x1            y1         x2            y2          site      depth
## 1 SFK001 -0.12000280  0.0086944453 -0.1479462 -0.0009289205 Tortugas Bank Mesophotic
## 2 SFK002 -0.11999040  0.0070304820 -0.1310021  0.0069938380 Tortugas Bank Mesophotic
## 3 SFK003 -0.13226702  0.0005281082 -0.1479462 -0.0009289205 Tortugas Bank Mesophotic
## 4 SFK004  0.06906114 -0.0593680913  0.1380109 -0.0344824197 Tortugas Bank Mesophotic
## 5 SFK005 -0.10683940  0.0156151506 -0.1479462 -0.0009289205 Tortugas Bank Mesophotic
## 6 SFK006  0.57063159 -0.0790486422  0.4430768 -0.0616296036 Tortugas Bank Mesophotic
#Calculate the angle of rotation, and then the slope of the rotated axis
theta = acos(symGenProcPlot$rotation[1,1]) 
slope = tan(theta)

#Calculate the y-intercepts for rotated axes
symGenProcInt = symGenProcPlot$translation[2] - (slope*symGenProcPlot$translation[1])
symGenProcInt2 = symGenProcPlot$translation[2] - (-1/slope*symGenProcPlot$translation[1])

sintSymGenProcPlotA = ggplot() +
  geom_hline(yintercept = 0, color = "gray90", linetype = 1) +
  geom_vline(xintercept = 0, color = "gray90", linetype = 1) +
  geom_abline(intercept = symGenProcInt, slope = slope, color = "gray75", linetype = 2) +
  geom_abline(intercept = symGenProcInt2, slope = -(1/slope), color = "gray75", linetype = 2) +
    geom_segment(data = symGenProcPlotData, aes(x = x2*(1-symGenProcPlot$scale), y = y2*(1-symGenProcPlot$scale), xend = x1*(1-symGenProcPlot$scale), yend = y1*(1-symGenProcPlot$scale), color = site), alpha = 0.5) +
  geom_point(data = symGenProcPlotData, aes(x = x2*(1-symGenProcPlot$scale), y = y2*(1-symGenProcPlot$scale), fill = site, shape = depth), alpha = 0.5)+
  geom_point(data = symGenProcPlotData, aes(x = x1*(1-symGenProcPlot$scale), y = y1*(1-symGenProcPlot$scale), fill = site, shape = depth), size = 2) +
  annotate(geom = "label", x = 0.172, y = 0.172, label = "           ", size = 10) +
  annotate(geom = "text", x = 0.172, y = 0.1825, label = "Procrustes analysis:", size = 3) +
  annotate(geom = "text", x = 0.172, y = 0.165, label = "italic(t[0]) == 0.919 *','~italic(p) < 0.0001", parse = TRUE, size = 3) +
  scale_color_manual(values = flPal) +
  scale_fill_manual(values = flPal, name = "Site") +
  scale_shape_manual(values = c(21, 23), name = "Depth zone") +
  guides(color = "none", fill = guide_legend(override.aes = list(shape = 15, color = flPal, size = 3), ncol =2, order = 1), shape = guide_legend(ncol = 1)) +
  theme_bw()

sintSymGenProcPlot = sintSymGenProcPlotA +
  theme(panel.grid = element_blank(),
        panel.border = element_rect(color = "black", size = 0.75, fill = NA),
        axis.title = element_blank(),
        axis.ticks = element_line(color = "black"),
        axis.text = element_text(color = "black", size = 8),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.box = "horizontal",
        legend.key = element_blank(),
        legend.background = element_blank(),
        legend.title = element_text(color = "black", size = 8),
        legend.text = element_text(color = "black", size = 8)
        )

sintSymGenProcPlot


S. intersepta genetic distance vs Symbiodiniaceae B-C distance

its2DistA = its2Profs %>% arrange(sample)
its2Dist = its2DistA[c(6:ncol(its2Profs))] %>% decostand("normalize") %>% vegdist(method = "bray")
its2PCoA = cmdscale(its2Dist, eig = TRUE, x.ret = TRUE)

sintIBS = read.table("../data/snps/sintFiltSnps.ibsMat")[-131,-131] %>% as.matrix() %>% as.dist(diag = FALSE)
sintPCoA = cmdscale(sintIBS, eig = TRUE, x.ret = TRUE)

set.seed(981) 
its2IBSProcrustes = protest(X = sintPCoA, Y = its2PCoA, permutations = 9999)
its2IBSProcrustes
## 
## Call:
## protest(X = sintPCoA, Y = its2PCoA, permutations = 9999) 
## 
## Procrustes Sum of Squares (m12 squared):        0.9519 
## Correlation in a symmetric Procrustes rotation: 0.2194 
## Significance:  0.0001 
## 
## Permutation: free
## Number of permutations: 9999
plot(its2IBSProcrustes)

plot(its2IBSProcrustes, kind = 2)

admixpops = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select("sample" = tubeID, "pop" = site, "depth" = depthZone)
admixpops$popdepth = as.factor(paste(admixpops$pop, admixpops$depth, sep = " "))

clumpp4 = read.table("../data/snps/k/ClumppK4.output", header = FALSE)
clumpp4$V1 = admixpops$sample

sintAdmix = admixpops[-131,] %>% left_join(clumpp4[,c(1, 6:9)], by = c("sample" = "V1"))

admixDist = sintAdmix[c(5:ncol(sintAdmix))] %>% vegdist(method = "euclidean")

admixPCoA = cmdscale(admixDist, eig = TRUE, x.ret = TRUE)

set.seed(981) 
its2AdmixProcrustes = protest(X = admixPCoA, Y = its2PCoA, permutations = 9999)

its2AdmixProcrustes
## 
## Call:
## protest(X = admixPCoA, Y = its2PCoA, permutations = 9999) 
## 
## Procrustes Sum of Squares (m12 squared):        0.9359 
## Correlation in a symmetric Procrustes rotation: 0.2531 
## Significance:  0.0001 
## 
## Permutation: free
## Number of permutations: 9999
plot(its2AdmixProcrustes, kind = 1)

plot(its2AdmixProcrustes, kind = 2)


Cheking dispersion with PERMDISP

Using vegan::betadisper() to look at multivariate homogeneity of dispersion (PERMDISP) between sites and depths. This is using Bray-Curtis dissimilarity.

alpha = with(its2Profs, tapply(specnumber(its2Profs[, c(6:ncol(its2Profs))]), site, mean))
alpha
##  Riley's Hump Tortugas Bank    Lower Keys    Upper Keys 
##      1.177778      1.290909      1.237288      1.200000
gamma = with(its2Profs, specnumber(its2Profs[, c(6:ncol(its2Profs))], site))
gamma
##  Riley's Hump Tortugas Bank    Lower Keys    Upper Keys 
##            13            11            11             9
gamma/alpha
##  Riley's Hump Tortugas Bank    Lower Keys    Upper Keys 
##     11.037736      8.521127      8.890411      7.500000
set.seed(694) 
its2dispS = betadisper(vegdist(decostand(its2Profs[, c(6:ncol(its2Profs))], "normalize")), its2Profs$site)

set.seed(694) 
permutest(its2dispS, permutations = 9999)
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 9999
## 
## Response: Distances
##            Df Sum Sq  Mean Sq      F N.Perm Pr(>F)
## Groups      3 0.0892 0.029731 0.7719   9999  0.516
## Residuals 215 8.2815 0.038518

No significant effect of Site on betadiversity.

alpha = with(its2Profs, tapply(specnumber(its2Profs[, c(6:ncol(its2Profs))]), depthZone, mean))
alpha
##    Shallow Mesophotic 
##   1.310924   1.130000
gamma = with(its2Profs, specnumber(its2Profs[, c(6:ncol(its2Profs))], depthZone))
gamma
##    Shallow Mesophotic 
##         18         11
gamma/alpha
##    Shallow Mesophotic 
##  13.730769   9.734513
set.seed(694)
its2dispD = betadisper(vegdist(decostand(its2Profs[, c(6:ncol(its2Profs))], "normalize")), its2Profs$depthZone)

its2dispD
## 
##  Homogeneity of multivariate dispersions
## 
## Call: betadisper(d = vegdist(decostand(its2Profs[, c(6:ncol(its2Profs))],
## "normalize")), group = its2Profs$depthZone)
## 
## No. of Positive Eigenvalues: 28
## No. of Negative Eigenvalues: 27
## 
## Average distance to median:
##    Shallow Mesophotic 
##     0.6525     0.5770 
## 
## Eigenvalues for PCoA axes:
## (Showing 8 of 55 eigenvalues)
##  PCoA1  PCoA2  PCoA3  PCoA4  PCoA5  PCoA6  PCoA7  PCoA8 
## 26.118 18.773 13.961  9.281  6.898  6.102  3.901  3.190
set.seed(694)
its2dispDPerm = permutest(its2dispD, permutations = 9999)

its2dispDPerm
## 
## Permutation test for homogeneity of multivariate dispersions
## Permutation: free
## Number of permutations: 9999
## 
## Response: Distances
##            Df Sum Sq  Mean Sq      F N.Perm Pr(>F)   
## Groups      1 0.3104 0.310449 10.815   9999 0.0013 **
## Residuals 217 6.2290 0.028705                        
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Depth does significantly affect beta diversity.

Running PERMANOVA in R

Now let’s see how different communities are from each other with PERMANOVA. We will utilize the vegan::adonis() function. We will use Bray-Curtis similarity for our distance matrix and run a total 0f 9,999 permutations, and test the effects of Site, Depth, and the interaction between Site and Depth.

set.seed(694)
its2Adonis = adonis2(decostand(its2Profs[, c(6:ncol(its2Profs))], "normalize") ~ site * depthZone, data = its2Profs, permutations = 9999, method = "bray")

its2Adonis 
## Permutation test for adonis under reduced model
## Terms added sequentially (first to last)
## Permutation: free
## Number of permutations: 9999
## 
## adonis2(formula = decostand(its2Profs[, c(6:ncol(its2Profs))], "normalize") ~ site * depthZone, data = its2Profs, permutations = 9999, method = "bray")
##                 Df SumOfSqs      R2       F Pr(>F)    
## site             3    8.455 0.09229  8.4735 0.0001 ***
## depthZone        1    5.368 0.05860 16.1410 0.0001 ***
## site:depthZone   3    7.612 0.08309  7.6290 0.0001 ***
## Residual       211   70.178 0.76602                   
## Total          218   91.613 1.00000                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
set.seed(694)
its2PWAdonis = pairwise.adonis(decostand(its2Profs[,c(6:ncol(its2Profs))], "normalize"), factors = its2Profs$site, sim.method = "bray", p.adjust.m = "fdr", perm = 9999)

its2PWAdonis
##                           pairs Df SumsOfSqs   F.Model         R2 p.value p.adjusted
## 1 Riley's Hump vs Tortugas Bank  1 3.0054485  7.606522 0.07202701  0.0001    0.00015
## 2    Riley's Hump vs Lower Keys  1 4.5770266 11.695628 0.10286788  0.0001    0.00015
## 3    Riley's Hump vs Upper Keys  1 3.4373577  9.258215 0.08247250  0.0001    0.00015
## 4   Tortugas Bank vs Lower Keys  1 0.9811441  2.446466 0.02137651  0.0334    0.03340
## 5   Tortugas Bank vs Upper Keys  1 1.6940679  4.427003 0.03770004  0.0014    0.00168
## 6      Lower Keys vs Upper Keys  1 3.4239083  9.014882 0.07153823  0.0001    0.00015
##   sig
## 1  **
## 2  **
## 3  **
## 4   .
## 5   *
## 6  **
its2profsRxD = paste(its2Profs$site, its2Profs$depthZone, sep = " ")

set.seed(694)
its2PWAdonis2 = pairwise.adonis(decostand(its2Profs[, c(6:ncol(its2Profs))], "normalize"), factors = its2profsRxD, sim.method = "bray", p.adjust.m = "fdr", perm = 9999)

its2PWAdonis2
##                                                  pairs Df SumsOfSqs   F.Model
## 1      Riley's Hump Shallow vs Riley's Hump Mesophotic  1 0.6958548  1.851042
## 2        Riley's Hump Shallow vs Tortugas Bank Shallow  1 2.8030339  7.429995
## 3     Riley's Hump Shallow vs Tortugas Bank Mesophotic  1 3.0884300  8.640125
## 4           Riley's Hump Shallow vs Lower Keys Shallow  1 5.0368955 15.488866
## 5        Riley's Hump Shallow vs Lower Keys Mesophotic  1 4.6194798 13.467087
## 6           Riley's Hump Shallow vs Upper Keys Shallow  1 4.7577989 13.584574
## 7        Riley's Hump Shallow vs Upper Keys Mesophotic  1 2.2928012  6.845341
## 8     Riley's Hump Mesophotic vs Tortugas Bank Shallow  1 1.7078494  4.482170
## 9  Riley's Hump Mesophotic vs Tortugas Bank Mesophotic  1 1.1310176  3.195846
## 10       Riley's Hump Mesophotic vs Lower Keys Shallow  1 3.2155336 10.357235
## 11    Riley's Hump Mesophotic vs Lower Keys Mesophotic  1 1.8698060  5.584033
## 12       Riley's Hump Mesophotic vs Upper Keys Shallow  1 2.4146387  7.007461
## 13    Riley's Hump Mesophotic vs Upper Keys Mesophotic  1 0.4347921  1.342139
## 14   Tortugas Bank Shallow vs Tortugas Bank Mesophotic  1 2.6960612  7.456037
## 15         Tortugas Bank Shallow vs Lower Keys Shallow  1 1.9880105  6.041735
## 16      Tortugas Bank Shallow vs Lower Keys Mesophotic  1 3.3741943  9.729362
## 17         Tortugas Bank Shallow vs Upper Keys Shallow  1 3.5069150  9.905960
## 18      Tortugas Bank Shallow vs Upper Keys Mesophotic  1 3.2100015  9.476618
## 19      Tortugas Bank Mesophotic vs Lower Keys Shallow  1 5.1052811 16.781413
## 20   Tortugas Bank Mesophotic vs Lower Keys Mesophotic  1 0.3865517  1.192600
## 21      Tortugas Bank Mesophotic vs Upper Keys Shallow  1 1.2421221  3.741093
## 22   Tortugas Bank Mesophotic vs Upper Keys Mesophotic  1 1.2155556  3.855401
## 23         Lower Keys Shallow vs Lower Keys Mesophotic  1 6.2867594 21.368532
## 24            Lower Keys Shallow vs Upper Keys Shallow  1 6.0655965 20.114834
## 25         Lower Keys Shallow vs Upper Keys Mesophotic  1 6.1026797 21.338933
## 26         Lower Keys Mesophotic vs Upper Keys Shallow  1 1.5730255  4.919063
## 27      Lower Keys Mesophotic vs Upper Keys Mesophotic  1 2.7859051  9.149432
## 28         Upper Keys Shallow vs Upper Keys Mesophotic  1 3.3019234 10.593110
##            R2 p.value   p.adjusted sig
## 1  0.04127087  0.1058 0.1139384615    
## 2  0.11355640  0.0001 0.0002000000  **
## 3  0.14017047  0.0001 0.0002000000  **
## 4  0.21367234  0.0001 0.0002000000  **
## 5  0.18843760  0.0001 0.0002000000  **
## 6  0.18976958  0.0001 0.0002000000  **
## 7  0.10556412  0.0003 0.0004941176  **
## 8  0.09439691  0.0008 0.0011200000   *
## 9  0.07757691  0.0243 0.0272160000   .
## 10 0.19781860  0.0001 0.0002000000  **
## 11 0.11493555  0.0025 0.0033333333   *
## 12 0.14012831  0.0003 0.0004941176  **
## 13 0.03026780  0.2359 0.2446370370    
## 14 0.12332989  0.0002 0.0003733333  **
## 15 0.09583706  0.0008 0.0011200000   *
## 16 0.14365058  0.0001 0.0002000000  **
## 17 0.14587762  0.0001 0.0002000000  **
## 18 0.14044299  0.0001 0.0002000000  **
## 19 0.24398180  0.0001 0.0002000000  **
## 20 0.02200670  0.2878 0.2878000000    
## 21 0.06593269  0.0121 0.0147304348   .
## 22 0.06781063  0.0221 0.0257833333   .
## 23 0.27266725  0.0001 0.0002000000  **
## 24 0.26084260  0.0001 0.0002000000  **
## 25 0.27239244  0.0001 0.0002000000  **
## 26 0.07818081  0.0047 0.0059818182   *
## 27 0.13625479  0.0004 0.0006222222  **
## 28 0.15443402  0.0001 0.0002000000  **
its2PWAdonis2 %>% filter(p.adjusted > 0.05)
##                                               pairs Df SumsOfSqs  F.Model         R2
## 1   Riley's Hump Shallow vs Riley's Hump Mesophotic  1 0.6958548 1.851042 0.04127087
## 2  Riley's Hump Mesophotic vs Upper Keys Mesophotic  1 0.4347921 1.342139 0.03026780
## 3 Tortugas Bank Mesophotic vs Lower Keys Mesophotic  1 0.3865517 1.192600 0.02200670
##   p.value p.adjusted sig
## 1  0.1058  0.1139385    
## 2  0.2359  0.2446370    
## 3  0.2878  0.2878000
its2PWAdonis2Tab = its2PWAdonis2 %>% mutate(pairs = pairs, F.Model = round(F.Model, 3), R2 = round(R2,3), p.adjusted =  round(p.adjusted, 4)) %>%dplyr::select(-p.value, -sig, -SumsOfSqs, -Df) %>%
  flextable() %>%
  flextable::compose(part = "header", j = "pairs", value = as_paragraph("Comparison")) %>%
  flextable::compose(part = "header", j = "F.Model", value = as_paragraph(as_i("Pseudo-F"))) %>%
  flextable::compose(part = "header", j = "R2", value = as_paragraph(as_i("R2"))) %>%
  flextable::compose(part = "header", j = "p.adjusted", value = as_paragraph("p-value")) %>%
  flextable::font(fontname = "Times New Roman", part = "all") %>%
  flextable::fontsize(size = 10, part = "all") %>%
  flextable::bold(part = "header") %>%
  flextable::align(align = "left", part = "all") %>%
  flextable::autofit()

table4 = read_docx()
table4 = body_add_flextable(table4, value = its2PWAdonis2Tab)
print(table4, target = "../tables/table4.docx")



Symbiodiniaceae dbRDA

options(sdmpredictors_datadir="../data/snps/bioOracle", timeout = max(300, getOption("timeout")))

popData = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66, 68, 164, 166, 209, 211),] %>% dplyr::select(sample, site, depthZone, latDD, longDD, depthM)

its2DistA = its2Profs %>% dplyr::arrange(sample)
its2Dist = vegdist(decostand(its2DistA[, c(6:ncol(its2DistA))], "normalize"), method = "bray")
datasets = list_datasets(terrestrial = FALSE, marine = TRUE, freshwater = FALSE)

# Extract present-day data sets
present = list_layers(datasets) %>% dplyr::select(dataset_code, layer_code, name, units, description, contains("cellsize"), version) %>% filter(version == 22) %>% filter(layer_code %in% c("BO22_damean", "BO22_parmean", "BO22_ph", "BO22_curvelmax_bdmean", "BO22_salinitymean_bdmean", "BO22_salinitymean_ss", "BO22_curvelmean_ss", "BO22_curvelmean_bdmean", "BO22_dissoxmean_bdmean", "BO22_lightbotmax_bdmean", "BO22_lightbotmean_bdmean", "BO22_nitratemean_bdmean", "BO22_tempmax_bdmean", "BO22_tempmean_bdmean", "BO22_ppmean_ss", "BO22_ppmean_bdmean", "BO22_chlomean_ss", "BO22_chlomean_bdmean"))

envVar = load_layers(present$layer_code)

itsEnvData = data.frame(popData, raster::extract(envVar, popData[,5:4]))[-131,] %>% cbind(pcangsd[-131,c(6:8)])

corData = rcorr(as.matrix(itsEnvData[,c(6:ncol(itsEnvData))]), type = "pearson")
corDataFlat = melt(corData$r, value.name = "r")
pDataFlat = melt(corData$P, value.name = "p")
corDataBind = corDataFlat %>% left_join(pDataFlat, by = c("Var1","Var2"))

ggplot(corDataBind) +
  geom_tile(aes(x = Var1, y = Var2, fill = r)) +
  scale_fill_gradient2(low = "#3B9AB2FF", high = "#F21A00FF") +
  geom_text(data = filter(corDataBind, r >= 0.7, p < 0.05),aes(x = Var1, y = Var2, label = round(r, 2))) +
    geom_text(data = filter(corDataBind, r <= -0.7, p < 0.05),aes(x = Var1, y = Var2, label = round(r, 2))) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90))

itsEnvData2 = itsEnvData %>% dplyr::select("BO22_curvelmean_ss", "depthM", "BO22_lightbotmean_bdmean", "BO22_tempmean_bdmean", "BO22_ppmean_bdmean", "PC1", "PC2", "PC3")

corData2 = cor(itsEnvData2)
corMelt2 = melt(corData2)

ggplot(corMelt2) +
  geom_tile(aes(x = Var1, y = Var2, fill = value)) +
  scale_fill_gradient2(low = "#3B9AB2FF", high = "#F21A00FF") +
  geom_text(data = corMelt2[corMelt2$value >= 0.7,],aes(x = Var1, y = Var2, label = round(value, 2))) +
    geom_text(data = corMelt2[corMelt2$value <= -0.7,],aes(x = Var1, y = Var2, label = round(value, 2))) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90))

vif = diag(solve(cor(itsEnvData2)))
vif
##       BO22_curvelmean_ss                   depthM BO22_lightbotmean_bdmean 
##                 2.077111                 1.746855                 3.891481 
##     BO22_tempmean_bdmean       BO22_ppmean_bdmean                      PC1 
##                 3.432994                 3.477058                 1.085170 
##                      PC2                      PC3 
##                 1.118312                 1.017985
itsDbrda0 = dbrda(its2Dist ~ 1, data = itsEnvData2)
itsDbrda1 = dbrda(its2Dist ~ BO22_curvelmean_ss + depthM + BO22_lightbotmean_bdmean + BO22_tempmean_bdmean + BO22_ppmean_bdmean + Condition(PC1, PC2, PC3), data = itsEnvData2)
anova(itsDbrda1)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = its2Dist ~ BO22_curvelmean_ss + depthM + BO22_lightbotmean_bdmean + BO22_tempmean_bdmean + BO22_ppmean_bdmean + Condition(PC1, PC2, PC3), data = itsEnvData2)
##           Df SumOfSqs      F Pr(>F)    
## Model      5   13.179 7.2972  0.001 ***
## Residual 212   76.576                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RsquareAdj(itsDbrda1)
## $r.squared
## [1] 0.1438554
## 
## $adj.r.squared
## [1] 0.1247137
set.seed(092)
bestItsDbrda <- ordiR2step(itsDbrda0, itsDbrda1, direction = "forward")
## Step: R2.adj= 0 
## Call: its2Dist ~ 1 
##  
##                            R2.adjusted
## <All variables>             0.12471375
## + depthM                    0.07556852
## + BO22_curvelmean_ss        0.03225721
## + BO22_tempmean_bdmean      0.02022712
## + BO22_ppmean_bdmean        0.01667414
## + BO22_lightbotmean_bdmean  0.01473131
## <none>                      0.00000000
## + Condition(PC1, PC2, PC3)  0.00000000
## 
##          Df    AIC      F Pr(>F)   
## + depthM  1 974.13 18.821  0.002 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Step: R2.adj= 0.07556852 
## Call: its2Dist ~ depthM 
##  
##                            R2.adjusted
## <All variables>             0.12471375
## + BO22_curvelmean_ss        0.10492630
## + BO22_lightbotmean_bdmean  0.09605059
## + BO22_ppmean_bdmean        0.09267258
## + BO22_tempmean_bdmean      0.09002396
## <none>                      0.07556852
## + Condition(PC1, PC2, PC3)  0.07494747
## 
##                      Df    AIC      F Pr(>F)   
## + BO22_curvelmean_ss  1 968.05 8.1174  0.002 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Step: R2.adj= 0.1049263 
## Call: its2Dist ~ depthM + BO22_curvelmean_ss 
##  
##                            R2.adjusted
## + BO22_lightbotmean_bdmean   0.1255916
## <All variables>              0.1247137
## + BO22_tempmean_bdmean       0.1210367
## + BO22_ppmean_bdmean         0.1189578
## <none>                       0.1049263
## + Condition(PC1, PC2, PC3)   0.1024550
bestItsDbrda$anova
##                        R2.adj Df    AIC       F Pr(>F)   
## + depthM             0.075569  1 974.13 18.8206  0.002 **
## + BO22_curvelmean_ss 0.104926  1 968.05  8.1174  0.002 **
## <All variables>      0.124714                            
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Now create best model and look at variance partitioning and significance with ANOVA

its2Model = itsEnvData2 %>% dplyr::select("Depth" = depthM, "Current" = BO22_curvelmean_ss, PC1, PC2, PC3)

its2Dbrda = dbrda(its2Dist ~ Depth + Current + Condition(PC1, PC2, PC3), its2Model)

itsDbrdaVarPart = varpart(its2Dist, ~ depthM, ~ BO22_curvelmean_ss, data = itsEnvData2)

itsDbrdaDepth = dbrda(its2Dist ~ Depth, its2Model)
itsDbrdaCur = dbrda(its2Dist ~ Current, its2Model)

plot(itsDbrdaVarPart, Xnames = c("Depth", "Current"), bg =c(3:4) , digits = 2)

itsDbrdaVarPart
## 
## Partition of squared Bray distance in dbRDA 
## 
## Call: varpart(Y = its2Dist, X = ~depthM, ~BO22_curvelmean_ss, data =
## itsEnvData2)
## 
## Explanatory tables:
## X1:  ~depthM
## X2:  ~BO22_curvelmean_ss 
## 
## No. of explanatory tables: 2 
## Total variation (SS): 91.613 
## No. of observations: 219 
## 
## Partition table:
##                      Df R.squared Adj.R.squared Testable
## [a+c] = X1            1   0.07981       0.07557     TRUE
## [b+c] = X2            1   0.03670       0.03226     TRUE
## [a+b+c] = X1+X2       2   0.11314       0.10493     TRUE
## Individual fractions                                    
## [a] = X1|X2           1                 0.07267     TRUE
## [b] = X2|X1           1                 0.02936     TRUE
## [c]                   0                 0.00290    FALSE
## [d] = Residuals                         0.89507    FALSE
## ---
## Use function 'dbrda' to test significance of fractions of interest
set.seed(004)
anova(its2Dbrda)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = its2Dist ~ Depth + Current + Condition(PC1, PC2, PC3), data = its2Model)
##           Df SumOfSqs      F Pr(>F)    
## Model      2   10.084 13.607  0.001 ***
## Residual 215   79.671                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
set.seed(003)
anova(itsDbrdaDepth)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = its2Dist ~ Depth, data = its2Model)
##           Df SumOfSqs      F Pr(>F)    
## Model      1    7.312 18.821  0.001 ***
## Residual 217   84.302                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
set.seed(002)
anova(itsDbrdaCur)
## Permutation test for dbrda under reduced model
## Permutation: free
## Number of permutations: 999
## 
## Model: dbrda(formula = its2Dist ~ Current, data = its2Model)
##           Df SumOfSqs      F Pr(>F)    
## Model      1    3.362 8.2665  0.001 ***
## Residual 217   88.251                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Prepare data and plot dbRDA

itsRdaVar = round(its2Dbrda$CA$eig/sum(its2Dbrda$CA$eig)*100, 1)
head(itsRdaVar)
## MDS1 MDS2 MDS3 MDS4 MDS5 MDS6 
## 30.2 18.8 15.3  8.7  8.3  6.9
itsRdaVarFit = round(its2Dbrda$CCA$eig/sum(its2Dbrda$CCA$eig)*100, 1)
head(itsRdaVarFit)
## dbRDA1 dbRDA2 
##   76.1   23.9
sintI2P = read.csv("../data/stephanocoeniaMetaData.csv")[-c(66,68,164,166,133,209,211),] %>% dplyr::select("sample" = tubeID, "pop" = site, "depth" = depthZone)

sintI2P$popdepth = paste(sintI2P$pop, sintI2P$depth)

its2RdaPoints = as.data.frame(scores(its2Dbrda, choices=c(1:2))$sites)
its2RdaPoints$sample = sintI2P$sample
head(its2RdaPoints)
##        dbRDA1     dbRDA2 sample
## 1 -1.43751668  2.8149088 SFK001
## 2 -0.03023976 -0.7189597 SFK002
## 3 -0.03684632 -0.7202105 SFK003
## 4 -1.30458571  2.6305362 SFK004
## 5 -0.66413063 -2.7178081 SFK005
## 6 -0.33555196  2.0829687 SFK006
its2DbrdaData1 = sintI2P %>% left_join(its2RdaPoints)
## Joining with `by = join_by(sample)`
head(its2DbrdaData1)
##   sample           pop      depth                 popdepth      dbRDA1     dbRDA2
## 1 SFK001 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -1.43751668  2.8149088
## 2 SFK002 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.03023976 -0.7189597
## 3 SFK003 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.03684632 -0.7202105
## 4 SFK004 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -1.30458571  2.6305362
## 5 SFK005 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.66413063 -2.7178081
## 6 SFK006 Tortugas Bank Mesophotic Tortugas Bank Mesophotic -0.33555196  2.0829687
tail(its2DbrdaData1)
##     sample        pop      depth              popdepth      dbRDA1     dbRDA2
## 214 SFK215 Upper Keys Mesophotic Upper Keys Mesophotic -1.77269134  1.2599950
## 215 SFK216 Upper Keys Mesophotic Upper Keys Mesophotic -1.43648298  2.8197042
## 216 SFK217 Upper Keys    Shallow    Upper Keys Shallow -0.66094456 -2.7030275
## 217 SFK218 Upper Keys    Shallow    Upper Keys Shallow  0.88196501  0.8116747
## 218 SFK219 Upper Keys    Shallow    Upper Keys Shallow -0.66633323 -2.7280262
## 219 SFK220 Upper Keys    Shallow    Upper Keys Shallow -0.03195399 -0.6975144
its2EnvLoad = as.data.frame(its2Dbrda$CCA$biplot)
its2EnvLoad$var = row.names(its2EnvLoad) 
its2EnvLoad$var = its2EnvLoad$var %>% gsub(pattern = "`", replacement = "", x = its2EnvLoad$var)

its2DbrdaData = merge(its2DbrdaData1, aggregate(cbind(mean.x = dbRDA1, mean.y2 = dbRDA2)~popdepth, its2DbrdaData1, mean), by="popdepth") %>% left_join(gssProf %>% dplyr::select(sample, otu, count) %>% group_by(sample) %>% summarise(count = max(count)) %>% left_join(gssProf %>% dplyr::select(sample, otu, count)) %>% rename(sym = otu) %>% dplyr::select(-count))
## Joining with `by = join_by(sample, count)`
## Joining with `by = join_by(sample)`
its2DbrdaData$depth = factor(its2DbrdaData$depth)
its2DbrdaData$depth = factor(its2DbrdaData$depth, levels(its2DbrdaData$depth)[c(2,1)])
its2DbrdaData$pop = factor(its2DbrdaData$pop)
its2DbrdaData$pop = factor(its2DbrdaData$pop, levels(its2DbrdaData$pop)[c(4, 1, 3, 2)])

head(its2DbrdaData)
##                popdepth sample        pop      depth      dbRDA1     dbRDA2     mean.x
## 1 Lower Keys Mesophotic SFK101 Lower Keys Mesophotic -0.03008525 -0.6775244 -0.3002103
## 2 Lower Keys Mesophotic SFK102 Lower Keys Mesophotic -0.66157736 -2.7059632 -0.3002103
## 3 Lower Keys Mesophotic SFK103 Lower Keys Mesophotic -1.42878784  2.8554029 -0.3002103
## 4 Lower Keys Mesophotic SFK104 Lower Keys Mesophotic -0.03611070 -0.7167979 -0.3002103
## 5 Lower Keys Mesophotic SFK105 Lower Keys Mesophotic -0.66193637 -2.7045180 -0.3002103
## 6 Lower Keys Mesophotic SFK106 Lower Keys Mesophotic -0.66955381 -2.7429669 -0.3002103
##     mean.y2                             sym
## 1 -1.477962                     C3-C1-C3.10
## 2 -1.477962                        C3/C3.10
## 3 -1.477962 C1/C3-C42.2-C1dl-C3gl-C3gm-C3gk
## 4 -1.477962                     C3-C1-C3.10
## 5 -1.477962                        C3/C3.10
## 6 -1.477962                        C3/C3.10
its2DbrdaPlotA = ggplot() +
  geom_hline(yintercept = 0, color = "gray90", size = 0.5) +
  geom_vline(xintercept = 0, color = "gray90", size = 0.5) +
  geom_segment(data = its2EnvLoad, aes(x = 0, y = 0, xend = dbRDA1, yend = dbRDA2), color = "#F5065B", arrow = arrow(length = unit(0.15, "cm"), type = "open"), size = 0.65) +
  geom_point(data = its2DbrdaData, aes(x = dbRDA1, y = dbRDA2, fill = sym, shape = depth), color = "black", size = 2, alpha = 1, position = position_jitter(seed = 1, width = 0.075, height = 0.075)) +
  scale_shape_manual(values = c(21, 23), name = "Depth Zone") +
  geom_text(data = its2EnvLoad[1,], aes(x = dbRDA1-0.22, y = dbRDA2, label = var), color = "#F5065B", size = 3, fontface = "bold") +
  geom_text(data = its2EnvLoad[2,], aes(x = dbRDA1, y = dbRDA2+0.2, label = var), color = "#F5065B", size = 3, fontface = "bold") +
  scale_fill_manual(values = profPal[-c(3,6,7,8,18)], name = expression(paste(italic("ITS2"), " type profile"))) +
  labs(title = "Symbiodiniaceae", x = paste0("dbRDA 1 (", itsRdaVarFit[1], "% [",itsRdaVar[1], "%])"), y = paste0("dbRDA 2 (", itsRdaVarFit[2], "% [",itsRdaVar[2], "%])")) +
  
  guides(shape = guide_legend(override.aes = list(size = 3, stroke = 0.5, alpha = NA), order = 2), fill = guide_legend(override.aes = list(shape = 22, size = 4), order = 1))+
  
  theme_bw()

its2DbrdaPlot = its2DbrdaPlotA +
  theme(plot.title = element_text(color = "black", size = 12),
        axis.title.x = element_text(color = "black", size = 10),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line.x = element_blank(),
        axis.title.y = element_text(color = "black", size = 10),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank(),
        legend.spacing = unit(-5, "pt"),
        legend.key.size = unit(5, "pt"),
        legend.position = "right",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 8),
        panel.border = element_rect(color = "black", size = 1),
        panel.background = element_rect(fill = "white"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

# its2DbrdaPlot
dbRdaPlots = (sintDbrdaPlot / its2DbrdaPlot) + 
  plot_annotation(tag_levels = 'A') &
  theme(plot.tag = element_text(size = 16))
  
ggsave("../figures/figure4.png", plot = dbRdaPlots, height = 18, width = 18, units = "cm", dpi = 300)

ggsave("../figures/figure4.svg", plot = dbRdaPlots, height = 18, width = 18, units = "cm", dpi = 300)
save.image("fknmsSint.RData")

install.packages(“mclust”) library(mclust)

bams = read.csv(“../data/stephanocoeniaMetaData.csv”)[-c(66,68,164,166,209,211),] %>% dplyr::select(“sample” = tubeID, “pop” = site, “depth” = depthZone) # list of bams files and their populations (tech reps removed)

ma = as.matrix(read.table(“../data/snps/sintFiltSnps.ibsMat”)) # reads in IBS matrix produced by ANGSD # ma = as.matrix(read.table(“../data/snps/sintNoClones.ibsMat”)) # reads in IBS matrix produced by ANGSD

dimnames(ma) = list(bams[,1],bams[,1])

d_clust <- Mclust(ma, G=1:15, modelNames = mclust.options(“emModelNames”))

genad2 = cbind(gen_ad,d_clust$classification)